
© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning. ©NOT Jones FOR SALE & OR Bartlett DISTRIBUTION. Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Conditions, Logical Expressions, 5 and Selection Control Structures © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION To understand how the Boolean operators work. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLCKNOWLEDGE GOALS NOT FOR SALE ORTo DISTRIBUTION understand the flow of control in a branching statement.NOT FOR SALE OR DISTRIBUTION To understand the flow of control in a nested branching statement. To know what preconditions and postconditions are. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOTTo FOR be able SALE to: OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Construct a simple logical (Boolean) expression to evaluate a given condition. SKILL GOALS Construct a complex logical expression to evaluate a given condition. Construct an If-Then-Else statement to perform a specific task. © Jones & Bartlett Learning,Construct an If-Then LLC statement to perform a specific task.© Jones & Bartlett Learning, LLC NOT FOR SALE ORConstruct DISTRIBUTION a set of nested If statements to perform a specificNOT task. FOR SALE OR DISTRIBUTION Trace the execution of a C++ program. Test and debug a C++ program. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 185 © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 9781449694265_CH05_Final.indd 185 1/18/13 11:02:02 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning. ©NOT Jones FOR SALE & OR Bartlett DISTRIBUTION. Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 186 CHAPTER 5 Conditions, Logical Expressions, and Selection Control Structures So far, the statements in our programs have been executed in their physical order. The first ©statement Jones is & executed, Bartlett then Learning, the second, LLC and so on, until all of the statements© Jones have & beenBartlett ex- Learning, LLC ecuted. But what if we want the computer to execute the statements in some other order? SupposeNOT FOR we want SALE to check OR theDISTRIBUTION validity of input data and then performNOT a calculation FOR SALE or print OR DISTRIBUTION an error message, but not both. To do so, we must be able to ask a question and then, based on the answer, choose one or another course of action. The If statement allows us to execute statements in an order that is different from their physical order. We can ask a question with it and do one thing if the answer is yes (true) or © Jones & Bartlettanother thingLearning, if the answer LLC is no (false). In the first ©part Jones of this &chapter, Bartlett we deal Learning, with asking LLC NOT FOR SALEquestions; OR DISTRIBUTION in the second part, we deal with the If statementNOT FOR itself. SALE OR DISTRIBUTION 5.1 Flow of Control The order in which statements are executed in a program is called the flow of control. © Jones & BartlettFlow of control Learning, The order in LLC © Jones & Bartlett Learning, LLC which the computer executes In a sense, the computer is under the control of one statement at a time. When a NOT FOR SALEstatements OR in a program. DISTRIBUTIONstatement has been executed,NOT controlFOR SALE is turned OR over DISTRIBUTION to the next statement (like a Control structure A statement baton being passed in a relay race). used to alter the normally sequential Flow of control is normally sequential (see FIGURE 5.1). That is, when one state- flow of control. ment is finished executing, control passes to the next statement in the program. When we want the flow of control to be nonsequential, we use control structures, © Jonesspecial & Bartlett statements Learning, that transfer LLC control to a statement other© Jones than the & oneBartlett that Learning, LLC physicallyNOT FOR comes SALE next. OR Control DISTRIBUTION structures are so important that weNOT focus FOR on them SALE in the OR DISTRIBUTION remainder of this chapter and in the next four chapters. Selection We use a selection (or branching) control structure when we want the computer to choose © Jones & Bartlettbetween Learning, alternative actions. LLC Within the control structure,© Jones we make& Bartlett an assertion—a Learning, claim LLC NOT FOR SALEthat isOR either DISTRIBUTION true or false. We commonly refer to thisNOT assertion FOR as SALE the branching OR DISTRIBUTION condition. If the assertion is true, the computer executes one statement. If it is false, it executes another (see FIGURE 5.2). The computer’s ability to solve practical problems is a product of its ability to make decisions and execute different sequences of instructions. The LeapYear program in Chapter 1 shows the selection process at work. The computer © Jones & Bartlett Learning,must LLCdecide whether a year is a leap© year.Jones It does & Bartlett so by testing Learning, the assertion LLC that the year is NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION statement 1 © Jones & Bartlett Learning, LLC statement 2 © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTIONFlow of NOT FOR SALE OR DISTRIBUTION control statement 3 © Jones & Bartlett Learning, LLC statement© Jones 4 & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION FIGURE 5.1 Flow of Control © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 9781449694265_CH05_Final.indd 186 1/18/13 11:02:05 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning. ©NOT Jones FOR SALE & OR Bartlett DISTRIBUTION. Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 5.2 Conditions and Logical Expressions 187 © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION false true Assertion © Jones & Bartlett Learning,statement LLC 1B statement 1A© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION FIGURE 5.2 Selection (Branching) Control Strucuture © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE ORnot DISTRIBUTION divisible by 4. If the assertion is true, theNOT computer FOR followsSALE theOR instructions DISTRIBUTION to return false, indicating that the year is not a leap year. If the assertion is false, the computer goes on to check the exceptions to the general rule. In Chapter 1, we said that this construct is like a fork in a road. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FORBefore SALE we ORexamine DISTRIBUTION selection control structures in C++, let’sNOT look FOR closely SALE at how OR we DISTRIBUTIONget the computer to make decisions. 5.1.1 What does “flow of control” mean? (p. 186) © Jones & Bartlett Learning, 5.1.2 LLC What is the “normal” flow of control© Jonesfor a program? & Bartlett (p. 186) Learning, LLC NOT FOR SALE OR DISTRIBUTION 5.1.3 What control structure do we useNOT when we FOR want a SALE computer ORto choose DISTRIBUTION between alternative actions? (p. 186) 5.1.4 What does a branch allow the computer to do? (pp. 186–187) © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC 5.2 ConditionsNOT andFOR Logical SALE OR Expressions DISTRIBUTION NOT FOR SALE OR DISTRIBUTION To ask a question in C++, we don’t phrase it as a question; we state it as an assertion. If the assertion we make is true, the answer to the question is yes. If the statement is not true, the answer to the question is no. For example, if we want to ask, “Are we having spinach for © Jonesdinner & tonight?”Bartlett we Learning, would say, “WeLLC are having spinach for dinner© Jones tonight.” & Bartlett If the assertion Learning, LLC is true, the answer to the question is yes. If not, the answer is no. NOT FORSo, SALE asking ORquestions DISTRIBUTION in C++ means making an assertion thatNOT is eitherFOR trueSALE or false. OR TheDISTRIBUTION computer evaluates the assertion, checking it against some internal condition (the values stored in certain variables, for instance) to see whether it is true or false. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 9781449694265_CH05_Final.indd 187 1/18/13 11:02:05 AM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning. ©NOT Jones FOR SALE & OR Bartlett DISTRIBUTION. Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 188 CHAPTER 5 Conditions, Logical Expressions, and Selection Control Structures The bool Data Type In© C++,Jones the &bool Bartlett data type Learning, is a built-in LLC type consisting of just two© values, Jones the & constants Bartlett Learning, LLC NOTtrue and FOR false SALE. The reserved OR DISTRIBUTION word bool is short for Boolean (pronouncedNOT “BOOL-e-un”).FOR SALE OR1 DISTRIBUTION Boolean data is used for testing conditions in a program so that the computer can make decisions (with a selection control structure). We declare variables of type bool in the same way we declare variables of other types— that is, by writing the name of the data type and then an identifier: © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC bool dataOK; // True if the input data is valid NOT FOR SALEbool ORdone; DISTRIBUTION // True if the process is doneNOT FOR SALE OR DISTRIBUTION bool taxable; // True if the item has sales tax Each variable of type bool can contain one of two values: true or false.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages58 Page
-
File Size-