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
How can we upload a file in php?
What is meant by PEAR in PHP?
Why do we use php?
What is good average session duration?
Explain what is the function file_get_contents() usefull for?
Explain what does the function get_magic_quotes_gpc() means?
How to get the ip address of the client?
What is the difference between overloading and overriding in php?
What is the purpose of using php?
When sessions ends?
What is a string in r?
What is the difference between unset and unlink?
Who is the father of php and what is the current version of php and mysql?
What is php and how do you use it?
How can we check the value of a given variable is alphanumeric?