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 version of php do I have windows?
What is the use of $_server["php_self"] variable?
What is form action php?
What is php good for?
What is fetch array in php?
How do you call a constructor for a parent class?
Is php an api?
Which have the fastest execution between mysql_fetch_array() and mysql_fetch_assoc()
How to track no of user logged in?
What is the use of super-global arrays in php?
How to retrieve the original query string?
What is empty () in php?
Write a program to display reverse of any number?
How to genrate report in wordpress cms
What is meant by PEAR in PHP?