C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-07-2002, 01:16 AM   #1
Registered User
 
Join Date: Apr 2002
Posts: 18
Post for loop or while loop

last question that I am not sure how to answer on my study guide:

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   Reply With Quote
Old 05-07-2002, 02:03 AM   #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...
}
And that would work just as well as a for loop.. wouldn't it? I'f I'm wrong could somebody please correct me.
Da-Spit is offline   Reply With Quote
Old 05-07-2002, 02:28 AM   #3
Guest
 
Sebastiani's Avatar
 
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   Reply With Quote
Old 05-07-2002, 04:13 AM   #4
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,125
Re: for loop or while loop

Quote:
Originally posted by slamit93
last question that I am not sure how to answer on my study guide:

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?
If you are going to iterate a known number of times, you should use a for loop.

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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 11:36 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22