5. Loops and Relational Expressions

In this chapter you’ll learn about the following:

• The for loop

• Expressions and statements

• The increment and decrement operators: ++ and --

• Combination assignment operators

• Compound statements (blocks)

• The comma operator

• Relational operators: >, >=, ==, <=, <, and !=

• The while loop

• The typedef facility

• The do while loop

• The get() character input method

• The end-of-file condition

• Nested loops and two-dimensional arrays

Computers do more than store data. They analyze, consolidate, rearrange, extract, modify, extrapolate, synthesize, and otherwise manipulate data. Sometimes they even distort and trash data, but we’ll try to steer clear of that kind of behavior. To perform their manipulative miracles, programs need tools for performing repetitive actions and for making decisions. Of course, C++ provides such tools. Indeed, it uses the same for loops, while loops, do while loops, if statements, and switch statements that regular C employs, so if you know C, you can zip through this chapter and Chapter 6, “Branching Statements and Logical Operators.” (But don’t zip too fast—you don’t want to miss how cin handles character input!) These various program control statements often use relational expressions and logical expressions to govern their behavior. This chapter discusses loops and relational expressions, and Chapter 6 follows up with branching statements and logical expressions.

Introducing for Loops

Circumstances often call on a program to perform repetitive tasks, such as adding together the elements of an array one by one or printing some paean to productivity 20 times. The C++ for loop makes such tasks easy to do. Let’s look at a loop in Listing 5.1, see what it does, and then discuss how it works.

Listing 5.1. forloop.cpp


// forloop.cpp -- introducing the for loop
#include <iostream>
int main()
{
    using namespace std;
    int i;  // create a counter
//   initialize; test ; update
    for (i = 0; i < 5; i++)
        cout << "C++ knows loops. ";
    cout << "C++ knows when to stop. ";
    return 0;
}


Here is the output from the program in Listing 5.1:

C++ knows loops.
C++ knows loops.
C++ knows loops.
C++ knows loops.
C++ knows loops.
C++ knows when to stop.

This loop begins by setting the integer i to 0:

i = 0

This is the loop initialization part of the loop. Then in the loop test, the program tests whether i is less than 5:

i < 5

If it is, the program executes the following statement, which is termed the loop body:

cout << "C++ knows loops. ";

Then the program uses the loop update part of the loop to increase i by 1:

i++

The loop update part of the loop uses the ++ operator, called the increment operator. It increments the value of its operand by 1. (The increment operator is not restricted to for loops. For example, you can use i++; instead of i = i + 1; as a statement in a program.) Incrementing i completes the first cycle of the loop.

Next, the loop begins a new cycle by comparing the new i value with 5. Because the new value (1) is also less than 5, the loop prints another line and then finishes by incrementing i again. That sets the stage for a fresh cycle of testing, executing a statement, and updating the value of i. The process continues until the loop updates i to 5. Then the next test fails, and the program moves on to the next statement after the loop.

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

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