PDA

View Full Version : loop in c


amaresh92
Jan20-11, 07:06 AM
greetings,
what is the meaning of controlled loop and count controlled loop?
advanced thanks.

Mark44
Jan20-11, 09:23 AM
First off, here's an uncontrolled loop (also called an infinite loop).
while (1)
{
// do something
}

Here's an example of a controlled loop.
while (response != 'Y' && response != 'y')
{
printf("Do you want to continue?");
response = getch();
}
The loop above is controlled, but there is no way of knowing how many times it will run.

Finally, here is a count-controlled loop.
i = 0;
while (i < 5)
{
printf("%d\n", i);
i++;
}
This loop will run 5 times, and prints
0
1
2
3
4