During Thursday's class, we covered how to request and use the user's input to terminate a loop. The loop is terminated by what is known as a sentinel value. When the user enters the sentinel value, the program terminates loop execution.
Here is the source code we worked on during Thursday's class which we used to square numbers.
Here is a run of the code.
So, what is happening and how?
Look at the first request for input from the user. The prompt requests a value, x, to square or the sentinel value, zero. When the user enters the sentinel value, the program skips the body of the loop. When the user enters a non-zero value, the program enters the body of the loop where the code will square the value input.
Instead of merely printing out the square of x, we have the whole equation displayed. You get this nice, tidy output from the first PRINT command inside the loop body.
Before leaving the body of the loop, we again request input from the user. If we did not, the value of x could never change and that would generate an infinite loop - a loop with no end.
No matter how many times the user chooses to square a number (zero, one, or many), eventually the user will decide to quit. The sentinel value ends loop iterations and the program continues with any statements appearing after the loop. In this case, the PRINT statement saying "Bye".
Now that you know how to use a sentinel value to halt execution of a pre-test loop, you have all the tools you need to complete lab #4. Note, however, that in lab #4, the sentinel value is -999, not zero as in this example.