"For" loops Vs. "While" loops

Which do you prefer?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

42 Replies - 5537 Views - Last Post: 11 July 2011 - 09:32 PM

#1 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 670
  • Joined: 04-April 08

"For" loops Vs. "While" loops

Posted 15 May 2008 - 12:28 PM

So: For loops or While loops?

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?
Is This A Good Question/Topic? 0
  • +

Replies To: "For" loops Vs. "While" loops

#2 spearfish  Icon User is offline

  • Monkey in Training
  • member icon

Reputation: 10
  • View blog
  • Posts: 746
  • Joined: 10-March 08

Re: "For" loops Vs. "While" loops

Posted 15 May 2008 - 02:44 PM

Depends on the situation.

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

Was This Post Helpful? 0
  • +
  • -

#3 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: "For" loops Vs. "While" loops

Posted 15 May 2008 - 02:49 PM

I do.
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.
Was This Post Helpful? 0
  • +
  • -

#4 mocker  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 50
  • View blog
  • Posts: 466
  • Joined: 14-October 07

Re: "For" loops Vs. "While" loops

Posted 15 May 2008 - 03:45 PM

They are two different tools used for different situations. Your assertion that while loops are 'more logical' and 'more readable' is off. Maybe you are not used to using for loops and would rather write out the counter declarations and increments instead of using a commonly used format for doing so, but I don't consider that more logical in any sense. What exactly do you have to keep straight with a for loop? All it does is give you a format to setup your counter, which you would need to do anyways in a while loop, if you are using the for loop where it should be used.
Was This Post Helpful? 0
  • +
  • -

#5 spearfish  Icon User is offline

  • Monkey in Training
  • member icon

Reputation: 10
  • View blog
  • Posts: 746
  • Joined: 10-March 08

Re: "For" loops Vs. "While" loops

Posted 15 May 2008 - 04:06 PM

Just to add on; the FOR loops are actually a bit more readable in my opinion.

All of the counter declarations on one line; rather than all over the place :-)
Was This Post Helpful? 0
  • +
  • -

#6 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: "For" loops Vs. "While" loops

Posted 15 May 2008 - 05:29 PM

Personally, I like it when everything is in order from start to finish. Just my opinion.
Was This Post Helpful? 0
  • +
  • -

#7 Guest_Whizzy*


Reputation:

Re: "For" loops Vs. "While" loops

Posted 15 May 2008 - 06:22 PM

Give us an example.

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...
Was This Post Helpful? 0

#8 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5191
  • View blog
  • Posts: 26,901
  • Joined: 10-May 07

Re: "For" loops Vs. "While" loops

Posted 15 May 2008 - 06:25 PM

I prefer for loops, because the incrementor is set & used in the same line that you call it. So it cuts out one line in the functionality. However, in a situation where I need a while loop, obviously I'm going to use that instead.
Was This Post Helpful? 0
  • +
  • -

#9 Ambercroft  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 129
  • Joined: 05-January 07

Re: "For" loops Vs. "While" loops

Posted 15 May 2008 - 07:43 PM

As Spearfish said

Quote

Depends on the situation.

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.
Was This Post Helpful? 0
  • +
  • -

#10 RodgerB  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 66
  • View blog
  • Posts: 2,284
  • Joined: 21-September 07

Re: "For" loops Vs. "While" loops

Posted 16 May 2008 - 01:26 AM

I hate when programmers replace the need to use a for loop with a while statement, with a passion. For loops are much easier to read then some extra lines of code attempting to replicate a for loop, just because you can get your head around it.

While statements and For loops provide two totally different functions.
Was This Post Helpful? 0
  • +
  • -

#11 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: "For" loops Vs. "While" loops

Posted 16 May 2008 - 01:30 AM

:^: They are not the same and should not be interchanged for the heck of it.

I won't repeat what has already been trodden on.
Was This Post Helpful? 0
  • +
  • -

#12 manzoor  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 13
  • View blog
  • Posts: 468
  • Joined: 07-August 07

Re: "For" loops Vs. "While" loops

Posted 16 May 2008 - 05:30 AM

while loop can do the job of for loops but there are situations where for loops would fit best, those includes counting and iterating through a sequence (arrays)
Was This Post Helpful? 0
  • +
  • -

#13 Nova Dragoon  Icon User is offline

  • The Innocent Shall Suffer, Big Time
  • member icon

Reputation: 36
  • View blog
  • Posts: 6,169
  • Joined: 16-August 01

Re: "For" loops Vs. "While" loops

Posted 16 May 2008 - 06:24 AM

For loops allow for easier compiler optimizations of unrolling making your program faster without risking losing semantic safety.
Was This Post Helpful? 0
  • +
  • -

#14 RodgerB  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 66
  • View blog
  • Posts: 2,284
  • Joined: 21-September 07

Re: "For" loops Vs. "While" loops

Posted 16 May 2008 - 06:31 AM

Think of it like this. A for loop is a female. It is sexy, elegant.. While statements attempting to be like for loops is like cross dressing, it looks ugly, its misleading and confusing.

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.
Was This Post Helpful? 0
  • +
  • -

#15 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: "For" loops Vs. "While" loops

Posted 16 May 2008 - 06:49 AM

Haha, get the flame editor :evil:
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3