Actually some of the above answers are true only to the extent that that is how they different structures are **TYPICALLY** used.
If fact, logically speaking, a for loop and while loop are identical. For example, the following two constructs will do the exact same things:
initialization code
while (condition)
{
loop stuff
loop iteration
}
AND
for (initialization code; condition; loop iteration)
{
loop stuff
}
The "loop iteration" does NOT have to be an increment - it can be any valid C expression as a matter of fact. It just usually is incrementing or multiplying a number by some constant.
The main difference is that the for loop can be written in one line rather than three. My rule of thumb is to use a for loop if the three expressions are all quite simple (like under 80 characters total) otherwise I break it up into a while loop.
Just remember this - a for loop does not have to count, it is identical to a while loop.
Note however that a do {} while (); **IS** different in that the loop is always executed at least once (test is done at the end of the loop), whereas the regular while and the for loops test at the beginning of the loop.
2007-07-24 13:20:36
·
answer #1
·
answered by brunt 4
·
0⤊
1⤋
Hi. A for loop eventually stops, unless the programmer had meant to make it infinite. While loops on the other hand are used very often to create infinite loops.
Example:
for (int i = 0; i < 10; i++)
{
cout << " "<
}
In this example, every time i is less then 10, it will be printed onto the screen, and it will then be incremented. So the first time around, i will equal to 0. The loop condition will pass, and 0 will be printed upon the screen. The last condition to equal true would be i = 9. Once i is incremented, it will no longer be less then 10, but it will be equal to 10. The conditional check will fail, and the loop will end. So you will see:
0 1 2 3 4 5 6 7 8 9
For loops are very useful when dealing with arrays.
For example, initializing a character array:
char myArray[25];
for (int i = 0; i < 25; i++)
myArray[i] = '\0';
You can make a for loop run infinitely by doing this:
for (;;)
{
// infinite
}
While loops on the other hand run only when a condition is true, and break when a condition is false.
Example:
int i = 0;
while (i < 5)
{
cout << " "<
i++;
}
The above says, while i is less then 5, print i onto the screen, and increment i by 1. You can make while loops run infinitely by doing this:
while(1)
{
}
since any value other then a 0 is true, so the condition will always equal true. You can also do this:
while(true)
{
}
It is advised that when running an infinite loop, use the Windows API (I think) function: Sleep().
2007-07-24 08:10:27
·
answer #2
·
answered by Anonymous
·
0⤊
0⤋
Hello,
For loops are INCREMENTAL. They iterate through a set or range of objects or numbers, starting at a certain point.
A While loop stays in the loop until a CONDITION IS MET. This does not necessarily mean you reach a certain number or the end of a collection. For many reasons you can use the two loops interchangeably, but there are infinitely many reasons to choose one over the other.
What if you had to wait for an event to occur. You would have to use a while loop because you would basically tell the computer: "While this event has not occurred, Do this..."
I hope this clears it up a bit. This is a very general explanation of the differences but I think it illustrates the main point well enough.
Hope this Helps! Best of Luck!
2007-07-24 07:10:23
·
answer #3
·
answered by tpennetta 2
·
0⤊
0⤋
With a for loop, you normally set a counter and then process the routine that many times. You set a specific number of times to do the loop. If you need the routine run 10 times, use a for loop.
With a while loop, you continue to repeat the routine until a condition is meet. Such as if you need the routine to run until it makes a certain value less then 1. Each time through the routine you change the value somehow. As soon as it is least then 1, the routine ends. Just while if you do not know how many times you will need the routine, but do have a condition for stopping it.
2007-07-24 07:07:18
·
answer #4
·
answered by dewcoons 7
·
0⤊
0⤋
Hi,
A "For loop" uses a counter and a "While loop" lets you wait for a condition to be true or false, Hope this helps you.
2007-07-24 07:06:45
·
answer #5
·
answered by Anonymous
·
0⤊
0⤋
whilst loop repeats particular statements based on the difficulty checked for loop facilitates in initialisation of a variable and repeats particular statements in keeping with difficulty checked and likewise facilitates in increment/decrement of the variable
2016-10-09 08:23:53
·
answer #6
·
answered by Anonymous
·
0⤊
0⤋