"For" loops Vs. "While" loops

Which do you prefer?

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

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

#16 mikeblas  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 390
  • Joined: 08-February 08

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

Posted 16 May 2008 - 09:20 AM

View Postpolymath, on 15 May, 2008 - 05:29 PM, said:

Personally, I like it when everything is in order from start to finish. Just my opinion.

So you don't use loops?
Was This Post Helpful? 0
  • +
  • -

#17 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

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

Posted 16 May 2008 - 01:19 PM

Generally if I can I use a for loop. I do like using iterators (which tend to end up in while loops) but even there I often put them in for-loops.

I use which ever loop makes the most logical sense to me when I write it. I use for-loops for structured loops. I use while-loops for indefinite loops, I use do-while loops when I need the inner portion to run at least once... basically I choose which ever loop seem to make the most logical sense to me.
Was This Post Helpful? 0
  • +
  • -

#18 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1956
  • View blog
  • Posts: 4,058
  • Joined: 11-December 07

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

Posted 16 May 2008 - 03:36 PM

I prefer for loops but that's only because I learned to program on a Spectrum and the for loop was the only one built in. While loops looked something like:

10 INPUT "Type 's' to stop: "; a$
20 PRINT "You typed: "; a$
30 IF a$ <> "s" THEN GOTO 10

Brings back memories...

You can pervert either type of loop for any purpose but from a practical point of view the question only really makes sense when either type of loop is a reasonable choice. Imagine a scenario where you need a counter but the exit condition is not related to said counter.

int counter = 0;
while ( condition ) {
   // do stuff
   counter ++;
   }

for ( int i = 0; condition; i++ ) {
   // do stuff
   }

... or even ...

for ( int i = 0; true; i++ ) {
   if ( !condition ) {
	  break;
	  }
	  // do stuff
   }


The middle one is the most concise but I would shy away from it because the counter isn't directly related to the condition. The while loop is best practice in this case but I'm sure there are scenarios where one of the other two provides a more human-readable solution.
Was This Post Helpful? 0
  • +
  • -

#19 Overhed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 21-May 08

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

Posted 21 May 2008 - 11:23 AM

They're two different tools for different situations. If I find that I want to do something up until some numerically related condition, I'll use a for loop since it has the built-in counter.

Otherwise I'll use a while loop.
Was This Post Helpful? 0
  • +
  • -

#20 crcapps  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 60
  • Joined: 13-May 08

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

Posted 21 May 2008 - 12:14 PM

View PostAmbercroft, on 15 May, 2008 - 07:43 PM, said:

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.


Exactly. So often, the condition is indeed not a simple increment.


Let's also not forget the Do...While, nor the Foreach. Each loop has a very distinct purpose.

Another fact that has not been thus far broached, except indirectly just a little by Nova Dragoon, is that the types of loops may have a different low-level implementation. Just because it looks like it does the same thing at as high of a level as the 3GL language you are using, doesn't mean the assembler does the same thing when it turns your Fors and Whiles into ones and zeroes.
Was This Post Helpful? 0
  • +
  • -

#21 herefishyfishy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 60
  • Joined: 01-May 08

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

Posted 22 May 2008 - 03:32 PM

I don't remember the last time I've used a while loop. I use a for loop whenever I can. Even when I need to test for a condition I use a for loop sometimes:
for(;condition;){
statement;
}


or
for(;;){
if(!condition)
	break;
}

  • "for" is shorter than "while" and thus easier to type in.
  • For loops are more compact than while loops.
  • You don't have to type in many lines of code to cycle through an array - there's always the for...each (or for...in, if you prefer) loop.
  • For loops are much easier to read, in my opinion.

Was This Post Helpful? 0
  • +
  • -

#22 Winstinology  Icon User is offline

  • I'm pretty good with a laser mouse..
  • member icon

Reputation: 7
  • View blog
  • Posts: 958
  • Joined: 08-May 08

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

Posted 23 May 2008 - 05:47 PM

Forawhile yadyadayada Whileafor ayadayadayada... j/k.... I like while loops better.. but sreiously.. come on..
Was This Post Helpful? 0
  • +
  • -

#23 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 23 May 2008 - 06:00 PM

That has got to be the most ignorant reply the internet has ever witnessed.
Was This Post Helpful? 0
  • +
  • -

#24 KYA  Icon User is offline

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

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

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

Posted 23 May 2008 - 06:48 PM

God bless America!
Was This Post Helpful? 0
  • +
  • -

#25 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

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

Posted 25 May 2008 - 11:25 PM

View Postherefishyfishy, on 22 May, 2008 - 03:32 PM, said:

I don't remember the last time I've used a while loop. I use a for loop whenever I can. Even when I need to test for a condition I use a for loop sometimes:
for(;condition;){
statement;
}


or
for(;;){
if(!condition)
	break;
}
  • "for" is shorter than "while" and thus easier to type in.
  • For loops are more compact than while loops.
  • You don't have to type in many lines of code to cycle through an array - there's always the for...each (or for...in, if you prefer) loop.
  • For loops are much easier to read, in my opinion.



Generally you should not use constructions like for(;; ) its one of these lessons that the gurus of old pass on. Basically one should try to use the control structures as they were intended. You would only use for(;; ) if you had a loop that logically should never exit. That is what that means. When a programmer sees that he should be able to safely think "oh this loop goes on forever" and not have to go and scan though looking for break statements.

Code is a language and should make sense. I should be able to follow your logic without the use of analytical tools. You should say what you mean. If you can't say it using the structures as they were meant to be used, chances are you don't really know the language very well (in this case I mean coding in general not a particular programming language).

For example, I don't know English very well.
Was This Post Helpful? 0
  • +
  • -

#26 crcapps  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 60
  • Joined: 13-May 08

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

Posted 27 May 2008 - 08:15 AM

for(;;) should be while(true)
this is indeed the way of the guru of old.

One shouldn't be cycling through arrays with while loops anyway.



View PostNickDMax, on 25 May, 2008 - 11:25 PM, said:

View Postherefishyfishy, on 22 May, 2008 - 03:32 PM, said:

I don't remember the last time I've used a while loop. I use a for loop whenever I can. Even when I need to test for a condition I use a for loop sometimes:
for(;condition;){
statement;
}


or
for(;;){
if(!condition)
	break;
}
  • "for" is shorter than "while" and thus easier to type in.
  • For loops are more compact than while loops.
  • You don't have to type in many lines of code to cycle through an array - there's always the for...each (or for...in, if you prefer) loop.
  • For loops are much easier to read, in my opinion.



Generally you should not use constructions like for(;; ) its one of these lessons that the gurus of old pass on. Basically one should try to use the control structures as they were intended. You would only use for(;; ) if you had a loop that logically should never exit. That is what that means. When a programmer sees that he should be able to safely think "oh this loop goes on forever" and not have to go and scan though looking for break statements.

Code is a language and should make sense. I should be able to follow your logic without the use of analytical tools. You should say what you mean. If you can't say it using the structures as they were meant to be used, chances are you don't really know the language very well (in this case I mean coding in general not a particular programming language).

For example, I don't know English very well.

This post has been edited by crcapps: 27 May 2008 - 08:16 AM

Was This Post Helpful? 0
  • +
  • -

#27 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

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

Posted 27 May 2008 - 09:19 AM

Interesting.

As I understand it, FOR loops are used when you know exactly how many times the loop is to iterate, and WHILE loops are used when the loop needs to iterate while a condition is true (or until a condition is true).

So you should need both, depending on the situation.

(As someone has already said :hmmm: )
Was This Post Helpful? 0
  • +
  • -

#28 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 29 May 2008 - 06:35 PM

It seems the general thought is that for loops are better. That was a very interesting conversation to watch. By the way, that was pretty ignorant winsonology. I liked the lady-cross dresser metaphor, RodgerB. I don't replace for loops with while loops "with a passion" i simply prefer them in many circumstances.

Oh, and diddo on that last one, KYA!! :-p
Was This Post Helpful? 0
  • +
  • -

#29 gregz0012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-July 11

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

Posted 07 July 2011 - 11:30 AM

In many languages for loops tend to be slightly quicker (as in execution speed). Is this the case with C#?

This post has been edited by gregz0012: 07 July 2011 - 11:30 AM

Was This Post Helpful? 0
  • +
  • -

#30 griffinfujioka  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 32
  • Joined: 26-October 08

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

Posted 07 July 2011 - 02:14 PM

Depends on the situation for me as well. I love all loops equally!
Was This Post Helpful? 0
  • +
  • -

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