Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C++ For Dummies (2004) [eng].pdf
Скачиваний:
84
Добавлен:
16.08.2013
Размер:
8.09 Mб
Скачать

Chapter 5: Controlling Program Flow

73

Nesting Control Commands

Return to our PC-screen-repaint problem. Surely it must need a loop struc­ ture of some type to write each pixel from left to right on a single line. (Do Middle Eastern terminals scan from right to left? I have no idea.) What about repeatedly repainting each scan line from top to bottom? (Do PC screens in Australia scan from bottom to top? Beats me.) For this particular task, you need to include the left-to-right scan loop within the top-to-bottom scan loop.

A loop command within another loop is known as a nested loop. As an exam­ ple, you can modify the BreakDemo program into a program that accumulates any number of sequences. In this NestedDemo program, the inner loop sums numbers entered from the keyboard until the user enters a negative number. The outer loop continues accumulating sequences until the sum is 0. Here’s what it looks like:

//NestedDemo - input a series of numbers.

//Continue to accumulate the sum

//of these numbers until the user

//enters a 0. Repeat the process

//until the sum is 0.

#include <cstdio> #include <cstdlib> #include <iostream> using namespace std;

int main(int nNumberofArgs, char* pszArgs[])

{

// the outer loop

cout << “This program sums multiple series\n”

<<“of numbers. Terminate each sequence\n”

<<“by entering a negative number.\n”

<<“Terminate the series by entering two\n”

<<“negative numbers in a row\n”;

//continue to accumulate sequences

int accumulator; do

{

//start entering the next sequence

//of numbers

accumulator = 0;

cout << “Start the next sequence\n”;

// loop forever for(;;)

{

// fetch another number int value = 0;

74

Part I: Introduction to C++ Programming

cout << “Enter next number: “; cin >> value;

// if it’s negative...

if (value < 0)

{

// ...then exit break;

}

//...otherwise add the number to the

//accumulator

accumulator = accumulator + value;

}

// output the accumulated result...

cout << “The total for this sequence is “

<<accumulator

<<endl << endl;

//...and start over with a new sequence

//if the accumulated sequence was not zero

}while (accumulator != 0);

// we’re about to quit

cout << “Thank you” << endl;

//wait until user is ready before terminating program

//to allow the user to see the program results system(“PAUSE”);

return 0;

}

Switching to a Different Subject?

One last control statement is useful in a limited number of cases. The switch statement resembles a compound if statement by including a number of dif­ ferent possibilities rather than a single test:

switch(expression)

{

case c1:

// go here if the expression == c1 break;

case c2:

// go here if expression == c2 break;

default:

// go here if there is no match

}

Chapter 5: Controlling Program Flow

75

The value of expression must be an integer (int, long, or char). The case values c1, c2, and c3 must be constants. When the switch statement is encountered, the expression is evaluated and compared to the various case constants. Control branches to the case that matches. If none of the cases match, control passes to the default clause.

Consider the following example code snippet:

int choice;

cout << “Enter a 1, 2 or 3:”; cin >> choice;

switch(choice)

{

case 1:

// do “1” processing break;

case 2:

// do “2” processing break;

case 3:

// do “3” processing break;

default:

cout << “You didn’t enter a 1, 2 or 3\n”;

}

Once again, the switch statement has an equivalent, in this case multiple if statements; however, when there are more than two or three cases, the switch structure is easier to understand.

The break statements are necessary to exit the switch command. Without the break statements, control falls through from one case to the next. (Look out below!)

76

Part I: Introduction to C++ Programming

Part II

Becoming a

Functional C++

Programmer

In this part . . .

It’s one thing to perform operations such as addition and multiplication — even when we’re logical (AND and OR or other operations). It’s another thing to write

real programs. This section introduces the features neces­ sary to make the leap into programmerdom.

You’ll find the program BUDGET1 on the enclosed CD-ROM. This largish program demonstrates the concepts of functional programming. You may want to visit this pro­ gram and its documentation once you’ve mastered func­ tional programming concepts.