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
What is advanced php programming?
What is the use of pear in php?
How can we make a constant in php?
What is orm in php framework?
Is php front end?
What are the features of php 7?
How to get the total number of values in an array?
What is the difference between rest and soap?
What is session_start () in php?
What is the super method?
What are the rules to determine the “truth” of any value which is not already of the boolean type?
What is difference between compile time and run time polymorphism?
What are Routines?
Is age an interval or ratio?
What is PHP's configuration file called?