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 php artisan serve?
What happens when submit button is clicked?
What is the interface in php?
Is c similar to php?
What is the method to execute a php script from the command line?
What is the difference between get and post in php?
Explain me difference between mysql_connect and mysql_pconnect?
Why die is used in php?
Why should I learn php?
Tell me how do I check if a given variable is empty?
How long will it take to learn php?
Is age nominal or ordinal?
What is the w3c?
What are the advantages of object-oriented programming in php?
What will the ?getdate() function returns in PHP?