Home Segments Index Top Previous Next

210: Mainline

In Chapter 12, you learned about the while statement:

while (Boolean expression) 
  embedded statement 

The while reading pattern consists of a while statement with a read expression in the place reserved for a Boolean expression:

while (read expression) 
  embedded statement 

The value of a read expression, such as scanf ("%lf%i", &price, &number), normally is the number of successfully assigned arguments.

Thus, the following while statement will continue looping, as you type numbers, because the value of the call to scanf is 2, rather than 0:

while (2 == scanf ("%lf%i", &price, &number)) { 
  ... 
} 

When you decide to stop the loop, assuming that you are working with a typical version of the Unix or Dos operating systems, you can arrange for the scanf call to stop reading before all variables are assigned, thus stopping the while loop, by typing the appropriate keychord.

You are said to type a keychord whenever you hold down one key while you press another. For example, if you are working with a typical version of Unix, you tell the scanf function to stop reading by holding down the control key and pressing the d key, thereby typing the control-d keychord.