Unit 8 Research Assignment 1: Exploring the Difference between Using Count-Controlled Loops and While Loops Count-controlled: control variable (or loop counter) initial value of the control variable increment (or decrement) by which the control variable is modified each iteration through the loop condition that tests for the final value of the control variable
A count-controlled repetition will exit after running a certain number of times. The count is kept in a variable called an index or counter. When the index reaches a certain value (the loop bound) the loop will end.
Count-controlled repetition is often called definite repetition because the number of repetitions is known before the loop begins executing. When we do not know in advance the number of times we want to execute a statement, we cannot use count-controlled repetition. In such an instance, we would use sentinel-controlled repetition.
(http://www.cs.iit.edu/~cs561/cs115/looping/count.html)
While Loop:
The while loop is used to repeat a section of code an unknown number of times until a specific condition is met. For example, say we want to know how many times a given number can be divided by 2 before it is less than or equal to 1. If we know a specific number, such as 32, we can say 5 times, but for a given symbolic variable "NUMBER" which represents any number in the world, how many times is not known a priori (before hand). In this case, we could use a while loop to determine that answer:
The "pseudocode" for such an algorithm is: while the number is bigger than one keep dividing it by two. Additionally keep a count of how many times we do the division.
Why While Loops: Like all loops, "while loops" execute blocks of code over and over again.
The advantage to a while loop is that it will go (repeat) as often as necessary to accomplish its goal. while ( condition is true ) do something % Note: the "something" should eventually result