# "For" loops Vs. "While" loops

• (3 Pages)
• 1
• 2
• 3

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

### #16 mikeblas

• D.I.C Regular

Reputation: 44
• Posts: 390
• Joined: 08-February 08

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

Posted 16 May 2008 - 09:20 AM

polymath, 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?

### #17 NickDMax

Reputation: 2254
• 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.

### #18 cfoley

• Cabbage

Reputation: 2338
• Posts: 4,889
• 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.

### #19 Overhed

Reputation: 0
• 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.

### #20 crcapps

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

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

Posted 21 May 2008 - 12:14 PM

Ambercroft, 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.

### #21 herefishyfishy

Reputation: 7
• 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.

### #22 Winstinology

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

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

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

Posted 23 May 2008 - 05:47 PM

### #23 RodgerB

• D.I.C Lover

Reputation: 66
• 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.

### #24 KYA

• Wubba lubba dub dub!

Reputation: 3186
• Posts: 19,211
• Joined: 14-September 07

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

Posted 23 May 2008 - 06:48 PM

God bless America!

### #25 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

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

Posted 25 May 2008 - 11:25 PM

herefishyfishy, 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.

### #26 crcapps

Reputation: 0
• 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.

NickDMax, on 25 May, 2008 - 11:25 PM, said:

herefishyfishy, 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

### #27 OliveOyl3471

• Everybody's crazy but me!

Reputation: 135
• 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 )

### #28 polymath

Reputation: 52
• 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

### #29 gregz0012

Reputation: 0
• 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

### #30 griffinfujioka

Reputation: 3
• 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!