Answer

  1. The statements in a while loop are executed zero or more times. If the condition is false to begin with, the loop body will not be executed. In order to guarantee that this loop body will be executed at least once, we arrange for the loop condition to be true at the outset.
  2. Something in the body of a loop must be able to do something that can change the value of the loop's condition. Otherwise, the loop will run forever. In this loop body, the value of temp (and potentially the value of the condition) is changed by the ``scanf''.
  3. If the user never enters a positive number, the loop will run forever. There is no predefined upper limit on the number of times a while loop will repeat.
  4. You know at least one thing for certain immediately after a loop terminates: the loop condition is false. For the example loop, this means that temp is positive.

Return to lesson.



Joseph L. Zachary
Hamlet Project
Department of Computer Science
University of Utah