Understanding Controlled & Count Controlled Loops

  • Thread starter Thread starter amaresh92
  • Start date Start date
  • Tags Tags
    Count Loops
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 2K views
amaresh92
Messages
163
Reaction score
0
greetings,
what is the meaning of controlled loop and count controlled loop?
advanced thanks.
 
on Phys.org
First off, here's an uncontrolled loop (also called an infinite loop).
Code:
while (1)
{
  // do something
}

Here's an example of a controlled loop.
Code:
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.
Code:
i = 0;
while (i < 5)
{
  printf("%d\n", i);
  i++;
}
This loop will run 5 times, and prints
0
1
2
3
4