Title | Chapter 05 - Controlling Program Flow |
---|---|
Author | USER COMPANY |
Course | C++ Programming Techniques |
Institution | Charles Sturt University |
Pages | 15 |
File Size | 307.8 KB |
File Type | |
Total Downloads | 52 |
Total Views | 135 |
Controlling Program Flow...
Chapter 5
Controlling Program Flow In This Chapter 䊳 Controlling the flow through the program 䊳 Executing a group of statements repetitively 䊳 Avoiding infinite loops
T
he simple programs that appear in Chapters 1 through 4 process a fixed number of inputs, output the result of that calculation, and quit. However, these programs lack any form of flow control. They cannot make tests of any sort. Computer programs are all about making decisions. If the user presses a key, the computer responds to the command. For example, if the user presses Ctrl+C, the computer copies the currently selected area to the Clipboard. If the user moves the mouse, the pointer moves on the screen. If the user clicks the right mouse button with the Windows key depressed, the computer crashes. The list goes on and on. Programs that don’t make decisions are necessarily pretty boring. Flow-control commands allow the program to decide what action to take, based on the results of the C++ logical operations performed (see Chapter 4). There are basically three types of flow-control statements: the branch, the loop, and the switch.
Controlling Program Flow with the Branch Commands The simplest form of flow control is the branch statement. This instruction allows the program to decide which of two paths to take through C++ instructions, based on the results of a logical expression (see Chapter 4 for a description of logical expressions).
62
Part I: Introduction to C++ Programming In C++, the branch statement is implemented using the if statement: if (m > n) { // Path 1 // ...instructions to be executed if // m is greater than n } else { // Path 2 // ...instructions to be executed if not }
First, the logical expression m > n is evaluated. If the result of the expression is true, control passes down the path marked Path 1 in the previous snippet. If the expression is false, control passes to Path 2. The else clause is optional. If it is not present, C++ acts as if it is present but empty. Actually, the braces are optional (sort of) if there’s only one statement to execute as part of the if. If you lose the braces, however, it’s embarrassingly easy to make a mistake that the C++ compiler can’t catch. The braces serve as a guide marker; it’s much safer to include ’em. (If your friends try to entice you into not using braces, “Just say No.”) The following program demonstrates the if statement (note all the lovely braces): // BranchDemo - input two numbers. Go down one path of the // program if the first argument is greater than // the first or the other path if not #include #include #include using namespace std; int main(int nNumberofArgs, char* pszArgs[]) { // input the first argument... int arg1; cout > arg1; // ...and the second int arg2; cout > arg2; // now decide what to do: if (arg1 > arg2)
Chapter 5: Controlling Program Flow { cout...