The while Loop

The while statement is another way of affecting the order of program execution. This conditional statement executes the statement under its control as long as a certain condition is true. Such potentially repeated execution is called a loop; a loop controlled by a while statement is called, logically enough, a while loop. Figure 3.23 is a program that uses a while loop to challenge the user to guess a secret number from 0 to 9, and keeps asking for guesses until the correct answer is entered.

Figure 3.23. Using a while statement (codeasic04.cpp)
#include <iostream>
using namespace std;

int main()
{
    short Secret;
    short Guess;

    Secret = 3;

    cout << "Try to guess my number. Hint: It's from 0 to 9" << endl;
    cin >> Guess;

    while (Guess != Secret)
      {
      cout << "Sorry, that's not correct." << endl;
      cin >> Guess;
      }

    cout << "You guessed right!" << endl;

    return 0;
}

There are a few wrinkles here that we haven't seen before. Although the while statement itself is fairly straightforward, the meaning of its condition, !=, isn't intuitively obvious. However, if you consider the problem we're trying to solve, you'll probably come to the (correct) conclusion that != means “not equal”, since we want to keep asking for more guesses while the Guess is not equal to our Secret number.[39] Since there is a comparison operator that tests for “not equal”, you might wonder how to test for “equal”, as well. As is explained in some detail in the next chapter, in C++ we have to use == rather than = to compare whether two values are equal.

[39] Why do we need parentheses around the expression Guess != Secret? The conditional expression has to be in parentheses so that the compiler can tell where it ends, and the statement to be controlled by the while begins.

You might also be wondering whether an if statement with an else clause would serve as well as the while; after all, if is used to select one of two alternatives, and the else could select the other one. The answer is that this would allow the user to take only one guess before the program ends; the while loop lets the user try again as many times as needed to get the right answer.

This example also illustrates another use of the curly braces, { and }. The first one of these starts a logical section of a program, called a block, and the second one ends the block. Because the two statements after the while are part of the same block, they are treated as a unit; both are executed if the condition in the while is true, and neither is executed if it is false. A block can be used anywhere that a statement can be used, and is treated in exactly the same way as if it were one statement.

Now you should have enough information to be able to write a simple program of your own. Susan asked for an assignment to do just that:

Susan: Based on what you have presented in the book so far, send me a setup, an exercise for me to try to figure out how to program, and I will give it a try. I guess that is the only way to do it. I can't even figure out a programmable situation on my own. So if you do that, I will do my best with it, and that will help teach me to think. (Can that be?) Now, if you do this, make it simple, and no tricks.

Of course, I did give her the exercise she asked for (exercise 3), but also of course, that didn't end the matter. She decided to add her own flourish, which resulted in exercise 4. These exercises follow below.

However, you'll have to install the compiler before you can write a program. To do this, follow the instructions in the readme.txt file on the CD.

Once you have followed those instructions, including successfully compiling one of the sample programs, use a text editor such as Notepad to write the source code for your program. Save the source code as myprog.cpp. Then follow the instructions in the readme.txt file on the CD to compile the source code. The resulting program will be called myprog.exe. To run your program normally from a DOS prompt, make sure you are in the “dialogcode” directory, and then type the name of the program, without the extension. In this case, you would just type "myprog". You can also run the program under the debugger to see how it works in detail, by following the instructions in the readme.txt file on the CD.

Now here are the programs Susan came up with, along with some others that fall in the same category.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset