what is the diffrence between for and foreach?
Answer Posted / nishant lokhande
foreach loop is also called as advance for loop
foreach loop is used to display an elements in collection.
eg.
To print arry element
by using foreach loop
public class Demo
{
public static void main(String args[])
{
String[] data = { "tomato", "potato","onion" };
for (String s : data)
{
System.out.println(s);
}
}
}
By using for loop
public class Demo
{
public static void main(String args[])
{
String[] data = { "tomato", "potato","onion" };
for(Iterator it = data.iterator();it.hasnext();)
{
String data1 = (String)it.next();
System.out.println(data1.charAt(0));
}
}
}
Is This Answer Correct ? | 2 Yes | 3 No |
Post New Answer View All Answers
In php, how to redirect from one page to another page?
How does php and apache work?
Tell me what does the array operator '===' means?
What are the __construct() and __destruct() methods in a php class?
Why do we show php code in browser?
What exactly is PHP?
Should I learn php before wordpress?
Who created numbers?
Tell me what are the different types of errors in php?
How to uploaded files to a table?
Is php front end or back end?
How we can retrieve the data in the result set of mysql using php?
Tell me how to find the length of a string?
How to get the http request in php?
Who is the father of php?