A while
statement repeats a section of code over and over again as long as some condition is true. Here is an diagram:
More formally, here is the flow of execution for a while
statement:
False
or True
.False
, exit the while
statement and continue execution at the next statement.True
, execute each of the statements in the body and then go back to step 1.The body consists of all of the statements below the header with the same indentation.
This type of flow is called a loop because the third step loops back around to the top. Notice that if the condition is False
the first time through the loop, the statements inside the loop are never executed.
The body of the loop should change the value of one or more variables so that eventually the condition becomes False
and the loop terminates. Otherwise the loop will repeat forever. This is called an infinite loop. An endless source of amusement for computer scientists is the observation that the directions written on the back of the shampoo bottle (lather, rinse, repeat) create an infinite loop.
What you will notice here is that the while
loop is more work for you — the programmer — than the equivalent for
loop. When using a while
loop you have to control the loop variable yourself. You give it an initial value, test for completion, and then make sure you change something in the body so that the loop terminates. That also makes a while loop harder to read and understand than the equivalent for loop.
So, while you can implement definite iteration with a while loop, it’s not a good idea to do that. Use a for loop whenever it will be known at the beginning of the iteration process how many times the block of code needs to be executed.
Use a while loop when it is not known how many times the block of code needs to be executed. For example, if prompting the user for menu options until they select the Quit option, you have no idea how many selections the user will make before they select the Quit option. In this case, it makes sense to use a while loop.
Let’s look at an example of this:
While Loop Structure
The break
statement is like an emergency escape for a while
or for
loop: break
causes an immediate jump to the commands after the end of the loop body. Here is an example using break
: it reads all lines of input until it finds one that says "END"
.
Break Statement
The continue
statement makes you skip the rest of a loop, then repeat the body from the next round (usually called the next “iteration”).
Continue Statement
Extend the “Looping through all lines of input” example above (we’ve copied it for you) by adding a new feature: any line equal to SKIP should not be printed, and should not cause the counter to be increased. Run the program to see an example.
Skip Line
If a × b = n, we call a × b a factorization of n. In this exercise, write a program that takes a positive integer n from input, and then outputs all factorizations of n; you should follow the formatting given by the following example for n=10.
1 times 10 equals 10
2 times 5 equals 10
5 times 2 equals 10
10 times 1 equals 10
Factorization Exercise