"For" loops Vs. "While" loopsWhich do you prefer?
42 Replies - 4093 Views - Last Post: 11 July 2011 - 09:32 PM
#1
"For" loops Vs. "While" loops
Posted 15 May 2008 - 12:28 PM
This question sparked a spirited discussion amongst my friends, and i was wondering what everyone else thought.
Personally, I like while loops because they are more readable, and you can always add 2 lines at the end to give it the same functionality as a for loop. In my opinion, two quick lines doesn't outweigh simplicity and readability.
What about you?
Replies To: "For" loops Vs. "While" loops
#2
Re: "For" loops Vs. "While" loops
Posted 15 May 2008 - 02:44 PM
If you need to do something for a predefined amount of time; use a FOR loop.
If you need to do something for as long as something is true, use a WHILE loop.
I mean, I don't see the point of going:
$i = 0
while ($i < 10) {
// yada yada
$i++;
}
because that's what FOR loops are for.
You might need to bend those rules for MySQL, but that's pretty much it. (Something like this:)
// Wants to return the first 10 entries, but there may be less than 10 in there
while ( ($array = mysql_fetch_array($result)) && ($i < 10) ) {
// Do something with array
$i++;
}
This post has been edited by spearfish: 15 May 2008 - 02:46 PM
#3
Re: "For" loops Vs. "While" loops
Posted 15 May 2008 - 02:49 PM
While loops are more readable, and more logical. Although they mean the same thing, one takes about 2 more chars. It has to do with where you put the end statement. While loops show systematically what happens in order. For loops don't give you that luxury. They tell you a bunch of stuff up front and expect you to keep it all straight.
#4
Re: "For" loops Vs. "While" loops
Posted 15 May 2008 - 03:45 PM
#5
Re: "For" loops Vs. "While" loops
Posted 15 May 2008 - 04:06 PM
All of the counter declarations on one line; rather than all over the place :-)
#6
Re: "For" loops Vs. "While" loops
Posted 15 May 2008 - 05:29 PM
#7 Guest_Whizzy*
Re: "For" loops Vs. "While" loops
Posted 15 May 2008 - 06:22 PM
Show me a FOR loop where you need to keep everything in order, and then show us the same function using a While loop where you don't have to keep everything in order.
It has been my experience when writing either loop, it is up to you to keep it in order from start to finish, but hey... I could be wrong...
#8
Re: "For" loops Vs. "While" loops
Posted 15 May 2008 - 06:25 PM
#9
Re: "For" loops Vs. "While" loops
Posted 15 May 2008 - 07:43 PM
Quote
If you need to do something for a predefined amount of time; use a FOR loop.
If you need to do something for as long as something is true, use a WHILE loop.
If I use a FOR loop my index is not modified in the loop. Exceptions there are of course.
In a WHILE loop the condition has to be modified and it is likely that the condition is modified not as a simple increment.
#10
Re: "For" loops Vs. "While" loops
Posted 16 May 2008 - 01:26 AM
While statements and For loops provide two totally different functions.
#11
Re: "For" loops Vs. "While" loops
Posted 16 May 2008 - 01:30 AM
I won't repeat what has already been trodden on.
#12
Re: "For" loops Vs. "While" loops
Posted 16 May 2008 - 05:30 AM
#13
Re: "For" loops Vs. "While" loops
Posted 16 May 2008 - 06:24 AM
#14
Re: "For" loops Vs. "While" loops
Posted 16 May 2008 - 06:31 AM
If you like cross dressing males just because it is familiar territory to you then go right ahead. But you are part of the stereotype now man. Part of the stereotype.
#15
Re: "For" loops Vs. "While" loops
Posted 16 May 2008 - 06:49 AM
|
|

New Topic/Question
Reply



MultiQuote







|