![]() |
| | #1 |
| Registered User Join Date: Apr 2002
Posts: 18
| When should a while loop be used and when should a for loop be used? In my understanding, they basically do the exact same thing, and I can pretty much write either type of loop to do the exact same thing... I guess, is it just preferential to use a for loop when you only want the loop to go through a certain number of iterations, and you should use a while loop when you just want a particuliar condition to be met for the loop to keep running? |
| slamit93 is offline | |
| | #2 |
| Registered User Join Date: Apr 2002
Posts: 99
| Well I guess it's just a matter of preferance, since a while loop can do almost anything a for loop can. All you have to do is: Code: while (count < 6)
{
count++
//Blah Blah Blah...
}
|
| Da-Spit is offline | |
| | #3 |
| Guest Join Date: Aug 2001
Posts: 5,025
| Really it's just a matter of preference. For loops handle: initialize, condition, post-assignment(usually incrementing) While loops just handle condition and often post-assignment. int i; for( i = 0; i < 10; i++) { //...do } int i = 0; while(i < 10) { //...do i++; } Sometimes the incrementation takes place in the condition part of the while loop, but be careful, often the incrementation takes place first so that: int i = 0; while(i++ < 10) //...i = 1 before entering loop! As if ++i !! { //...do } ...must be rewritten: int i = -1; while(i++ < 10) //...i = 0 before entering loop! As if ++i . { //...do } ...this may depend on your compiler, though... Often the incrementation is done on a variable being worked on, and the above problem does not occur: int variable[10]; int i = 0; while(i < 10) { variable[i++] = i; //...no problem } Incidentally, a while loop written as a for loop would be: int i = -1; //...to avoid side-effect problem... for( ; i++ < 10; ) { //...do } |
| Sebastiani is offline | |
| | #4 | |
| Confused Join Date: Sep 2001 Location: Sweden
Posts: 3,125
| Re: for loop or while loop Quote:
If you don't know how many times you are going to loop, but know that it is at least 1, you should use a do-while loop. If you don't know how many times you are going to loop, and it might not even be 1 loop, you should use a while loop. As has been pointed out, you could use one 'loop-type' to do what the other do with a little tweaking and clever layout, but these are the general purposes.
__________________ MagosX.com Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime. | |
| Magos is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| nested loop, simple but i'm missing it | big_brother | C Programming | 19 | 10-23-2006 10:21 PM |
| While loop misbehaving (or misunderstanding) | mattAU | C Programming | 2 | 08-28-2006 02:14 AM |
| loop in a linked linked lists | kris.c | C Programming | 6 | 08-26-2006 12:38 PM |
| while loop help | bliznags | C Programming | 5 | 03-20-2005 12:30 AM |
| loop issues | kristy | C Programming | 3 | 03-05-2005 09:14 AM |