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
Where to put php files in apache server?
Which programming language does php resemble to?
What is sorting php?
How to find the length of a string?
What is php glob?
List some features of php that are deprecated in php7?
What is difference between static and final in php?
Explain about the $_GET variable of PHP?
Which php function will attach one file to another?
Tell me what is the use of mysql_real_escape_string() function?
What is file upload?
What are php expressions?
What are objects in php?
Tell me what does pear stands for?
Is empty in excel?