11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5Controlling Program FlowIn This Chapter Controlling the flow through the program Executing a group of statements repetitively Avoiding infinite loopsThe simple programs that appear in Chapters 1 through 4 process a fixednumber of inputs, output the result of that calculation, and quit. However,these programs lack any form of flow control. They cannot make tests of anysort. Computer programs are all about making decisions. If the user pressesa key, the computer responds to the command.For example, if the user presses Ctrl+C, the computer copies the currentlyselected area to the Clipboard. If the user moves the mouse, the pointermoves on the screen. If the user clicks the right mouse button with theWindows 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, theloop, and the switch.Controlling Program Flow with theBranch CommandsThe simplest form of flow control is the branch statement. This instructionallows 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 descriptionof logical expressions).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!