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


Please Help Members By Posting Answers For Below Questions

In php, how to redirect from one page to another page?

517


How does php and apache work?

537


Tell me what does the array operator '===' means?

532


What are the __construct() and __destruct() methods in a php class?

513


Why do we show php code in browser?

514






What exactly is PHP?

610


Should I learn php before wordpress?

499


Who created numbers?

534


Tell me what are the different types of errors in php?

517


How to uploaded files to a table?

563


Is php front end or back end?

512


How we can retrieve the data in the result set of mysql using php?

527


Tell me how to find the length of a string?

616


How to get the http request in php?

578


Who is the father of php?

565