C:Loops - While You Were Away (Page 2 of 4 )
While loops work by saying: while this is true, keep repeating through the loop. Let's look at a program to print some text:
#include <stdio.h>
int main()
{
int count;
count= 0;
while (count<10)
{
printf("Look a loop!n");
count++;
}
return(0);
}
This program will print the following:
Look a loop!
Look a loop!
Look a loop!
Look a loop!
Look a loop!
Look a loop!
Look a loop!
Look a loop!
Look a loop!
Look a loop!
Let's try something else. Change the value of count to 11 and run the program again.
Nothing happens, right? This is because the loop only executes while the criteria is true.
Otherwise it won't execute at all.
To fix this, if it indeed needs fixing, we can create a Do While loop.
Next: Do it While You Can >>
More BrainDump Articles
More By James Payne