While Loops


It's easier to demonstrate while loops than to explain them. They're really simple. listing 2.6 presents the code for a simple program that uses a while loop.

Listing 2.6. Using a while loop

 1    #include <iostream> 2    #include <stdlib.h> 3 4    using namespace std; 5 6    int main(int argc, char *argv[]) 7    { 8        int anInt; 9 10       // Prompt the user for an integer. 11       cout << "Please input an integer and press Enter: "; 12       cin >> anInt; 13 14       int i=0; 15       while (i < anInt) 16       { 17       cout << i++ << endl; 18       } 19 20       system("PAUSE"); 21       return 0; 22   } 

This short program illustrates how easy using while loops is. After declaring an integer variable on line 8, the program prompts the user and gets a value for the variable. Next, it declares another integer called i. For reasons that had to do with the structure of early programming languages (specifically, early versions of a language called FORTRAN), it's traditional to name your loop counters i, j, or k. I follow that style of coding, so you'll see me use variables called i, j, and k whenever I use loops. Normally, we want our variables to have more descriptive names than i, j, and k. However, with loop counters, we can use these names because everyone knows what they are. If programmers see variables named i, j, or k, they expect those variables to be loop counters.

Note

You can always initialize variables at the time you declare them. In fact, it's preferable to do so for most variables.


Line 14 of the program declares a loop counter called i and sets it to 0. On line 15, the program compares the value in i to the value in anInt. The comparison is in the parentheses after the C++ keyword while.

Comparisons, such as the one at the beginning of the while loop on line 15, can evaluate to true or false. Those are actual values in C++, just like numbers are. The condition on line 15 evaluates to TRue if the value in i is less than the value in anInt. If the condition evaluates to true, the body of the loop executes. The body of the loop is contained in the opening and closing braces on lines 16 and 18.

The body of the while loop in Listing 2.5 contains just one statement. This statement does quite a bit. Let's look at it part by part.

First, the statement on line 17 says the program is sending a value to the stream cout. The value it's sending is the value in the variable i. Next, the statement increments the value. Finally, it sends an endline out to the cout stream. That's a lot to do in one statement, but this kind of programming is common in C++.

You've probably already figured out what the output of this program looks like. Nevertheless, I've provided it for your reference in Figure 2.8

Figure 2.8. The output from the program in Listing 2.6.


In Figure 2.8, I entered the number 10 when the program prompted me for input. The loop caused the program to print out the values 09 with one value printed per line.

After executing the statement in the loop's body, the program performs the loop's test again. If it evaluates to TRue, the program executes the loop's body again. This continues until the condition evaluates to false. The condition evaluates to false if i is greater than or equal to the value in anInt. As soon as the condition evaluates to false, the loop stops.

It's important to note that I was careful about the number I typed in for the output in Figure 2.8. I specifically entered a number that would make the loop execute. The while loop executes only for as long as its condition evaluates to TRue. If I had typed in 0 or a negative number, the value in i would not have been less than anInt. The condition on the while loop would have evaluated to false the first time it was tested. As a result, the loop would never have executed. Figure 2.9 demonstrates this.

Figure 2.9. The while loop won't execute if its condition is never true.


Because I typed in 0 when I ran the program this time, the while loop's condition evaluated to false right away. As a result, the loop's body was never executed.

Note

The statement on line 17 of Listing 2.5 uses a particular type of increment operator called a postincrement. A postincrement operator performs its increment after the value in the variable has been used in the statement. So, for example, if the statement a=b++; appears in a program, it tells the program to assign the value in b to the variable a and then perform the increment. Because the increment is performed after the value has been used, it is called a postincrement.




Creating Games in C++(c) A Step-by-Step Guide
Creating Games in C++: A Step-by-Step Guide
ISBN: 0735714347
EAN: 2147483647
Year: N/A
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net