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 the default session out time?
What is str_replace()?
What is an anti csrf token?
Tell me what is the difference between unset() and unlink()?
How do I know my xampp version?
What are the popular frameworks in php?
Can you explain, when to use if-else if-else over switch statements?
Why print_r is used in php?
Explain what does the function get_magic_quotes_gpc() means?
Is null function in php?
Tell me what is the importance of "action" attribute in a html form?
What type of headers that PHP supports?
How do I check if a given variable is empty?
What is Apache's configuration file typically called?
What are getters and setters php?