I'm reading an ebook on PHP right now, and the author noted that the difference between a while loop and a for loop is that the for loop will count how many times it runs.
So take this:
<?php
for ($i = 1; $i < 10; $i = $i + 1) {
print "Number $i\n";
}
?>
But wouldn't this be the same as
<?php
$i = 1;
while ($i < 10) {
$i = $i + 1;
print "Number $i\n";
}
?>
Or is there some other differences that he didn't point out? (Aside from using while loop for when you're unsure of how long the condition will remain true, such as selecting rows from a database)
I mean, if that's the only difference, can't I just not use the for loop and use the while loop instead?
Both for loop and while loop is used to execute the statements repeatedly while the program runs. The major difference between for loop and the while loop is that for loop is used when the number of iterations is known, whereas execution is done in the while loop until the statement in the program is proved wrong.
The for and foreach are the types of loops used for the different purposes in PHP where foreach is used for implementing associative arrays and for is used with variables. The for loop works by the end of the loop condition. On the other hand, the foreach loop works at the end of the array count.
Summary – for vs while Loop There are repetition control structures to achieve this tasks. Two of them are for and while loop. The difference between for and while loop is that the for loop is used when the number of iterations is known and the while loop is used when the number of iterations is not known.
While loop checks the condition first and then executes the statement(s), whereas do while loop will execute the statement(s) at least once, then the condition is checked. While loop is entry controlled loop, whereas do while is exit controlled loop.
Functionally, a for
loop is equivalent to a while
loop; that is, each can be rewritten as the other with no change to the outcome or side effects. However, each has different connotations. A while
loop runs while a condition holds; the condition is static, though circumstances change. A for
loop runs over a sequence. The difference is important to programmers but not programs, just as choice of variables names are important to programmers even though they can be changed to produce functionally equivalent code. One loop construct will make more sense than the other, depending on the situation.
The Main Difference Between for() and While() is that we have to define the limit or count but in while() loop we don't define limit or count it works until reached the last item
FOR LOOP Initialization may be either in loop statement or outside the loop. It is normally used when the number of iterations is known. Condition is a relational expression. It is used when initialization and increment is simple. for ( init ; condition ; iteration ) { statement(s); }
WHILE LOOP Initialization is always outside the loop. It is normally used when the number of iterations is unknown. Condition may be expression or non-zero value. It is used for complex initialization. while ( condition ) { statement(s); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With