what is the diffrence between for and foreach?
Answers were Sorted based on User's Feedback
Answer / web developer
for loop is to do something over and over again until the
task has been completed.
foreach works only on arrays. even it will run the loop
until end of the array.
Is This Answer Correct ? | 120 Yes | 8 No |
Answer / ajay jain
for() is a simply a loop which is execute to the given
condition but foreach given as array parameter return as
content of array
Is This Answer Correct ? | 22 Yes | 5 No |
Answer / ks.tarun
FOR loop will continue until some condition fails, but in
case of foreach loop will work until all item are processed.
Is This Answer Correct ? | 18 Yes | 5 No |
Answer / jennifer
A for loop is run on a condition, and will run until the
condition is false. Usually this is a counter, and the
condition is a threshold. (It can also easily be run
backwards, using $i-- instead of $i++.)
- so if the loop is "for ($i = 0; $i < 10; $i++)", it will
run 10 times, with $i beieng equal to 0, then 1, then
2, ... then 9, then when it reaches 10 the condition is
false and it jumps out to the next statement after the loop.
It can also be run with a condition unrelated to the
counter, or with non-counter-related statements, but
usually a while loop is better for that (and the
possibility of an accidental endless loop is much greater).
- like "for ($size = 0; $bag == 'full'; $size += 5)" which
assumes that somewhere (hopefully) the variable $bag gets
set to 'full' within the loop at some point. $i is just
counting the number of times the loop runs (or number of
items in the bag, in this case).
A foreach runs through an array (counted or associative),
executing the code in the the loop once for each element of
the array, with the array element (and optionally, the
associated key) available as variables.
- so "foreach ($lineup as $person)" will run the code in
the loop once for each person.
- "foreach ($lineup as $position => $person)" will do the
same thing but make the $position variable available,
especially useful for associative arrays.
Is This Answer Correct ? | 13 Yes | 4 No |
Answer / krunal gohel
for loop is to do something over and over again until the
task has been completed.
foreach works only on arrays. even it will run the loop
until end of the array.
Is This Answer Correct ? | 15 Yes | 6 No |
foreach is used to iterate over arrays and for loop is used
to run statement for number of times and there is another
major difference is in foreach you want to execute statement
which comes from an database with id which is
autoincremented and you run for loop over it and suppose in
the mean time i delete 3rd rows i.e. 3rd id than what will
this loop stops working but not in foreach and foreach is
the faster than for loop..
Is This Answer Correct ? | 3 Yes | 2 No |
Answer / swapnil
well when we use for loop it will execute only for the time
we have mentioned in loop ie ex for(i=0;i<25;i++) 25 times
whereas foreach is used only when we dont know that limit it
may be the case when we perform operation on database queries
Is This Answer Correct ? | 6 Yes | 6 No |
Answer / p parimi
For can be used to run statements for a fixed number of
times, foreach can be used to run statements for dynamically
generated arrays(may be result of database query).We can
also used for loop for dynamically generated array(may be
result of database query) but foreach is the ideal way to
iterate dynamic array. The reason behind this, we don't know
the index of array as it may be associate result from
database query. So if we use foreach than it will iterate
the result one by one.
Is This Answer Correct ? | 1 Yes | 1 No |
Answer / 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 |
Answer / anuradha
foreach sort of asbtracts away some of the complexity and is
usually easier. I use this whenever I don't need to know the
numeral index of the array or $key => $value won't provide
me with it.
for is the older C style where you must first perform a
count() so you know how many iterations the loop requires.
It is useful when you need to know the index, or to count
backwards or step through in different groups.
Is This Answer Correct ? | 1 Yes | 3 No |
What is $_ request in php?
Can the value of a constant change during the script's execution?
What is difference between compile time and run time polymorphism?
Is a number php?
What is input sanitization in php?
How to turn on the session support?
How to take a substring from a given string?
How to create and destroy cookies in php?
What is difference between echo and print in php?
What does explode do in php?
What is mysqli_fetch_array?
What are the Merits and De-Merits of CURL library ?