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

What is the default session out time?

542


What is str_replace()?

535


What is an anti csrf token?

496


Tell me what is the difference between unset() and unlink()?

530


How do I know my xampp version?

511






What are the popular frameworks in php?

517


Can you explain, when to use if-else if-else over switch statements?

533


Why print_r is used in php?

529


Explain what does the function get_magic_quotes_gpc() means?

510


Is null function in php?

562


Tell me what is the importance of "action" attribute in a html form?

529


What type of headers that PHP supports?

610


How do I check if a given variable is empty?

523


What is Apache's configuration file typically called?

553


What are getters and setters php?

539