Bài giảng C++ Programming: From Problem Analysis to Program Design, Fourth Edition - Chapter 4: Control Structures I (Selection)

Tài liệu Bài giảng C++ Programming: From Problem Analysis to Program Design, Fourth Edition - Chapter 4: Control Structures I (Selection): C++ Programming: From Problem Analysis to Program Design, Fourth EditionChapter 4: Control Structures I (Selection)C++ Programming: From Problem Analysis to Program Design, Fourth Edition2ObjectivesIn this chapter, you will:Learn about control structuresExamine relational and logical operatorsExplore how to form and evaluate logical (Boolean) expressionsDiscover how to use the selection control structures if, if...else, and switch in a programLearn to use the assert function to terminate a programC++ Programming: From Problem Analysis to Program Design, Fourth Edition3Control StructuresA computer can proceed:In sequenceSelectively (branch) - making a choiceRepetitively (iteratively) - loopingSome statements are executed only if certain conditions are met A condition is met if it evaluates to trueC++ Programming: From Problem Analysis to Program Design, Fourth Edition4Control Structures (continued)C++ Programming: From Problem Analysis to Program Design, Fourth Edition5Relational Oper...

ppt71 trang | Chia sẻ: honghanh66 | Lượt xem: 722 | Lượt tải: 0download
Bạn đang xem trước 20 trang mẫu tài liệu Bài giảng C++ Programming: From Problem Analysis to Program Design, Fourth Edition - Chapter 4: Control Structures I (Selection), để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên
C++ Programming: From Problem Analysis to Program Design, Fourth EditionChapter 4: Control Structures I (Selection)C++ Programming: From Problem Analysis to Program Design, Fourth Edition2ObjectivesIn this chapter, you will:Learn about control structuresExamine relational and logical operatorsExplore how to form and evaluate logical (Boolean) expressionsDiscover how to use the selection control structures if, if...else, and switch in a programLearn to use the assert function to terminate a programC++ Programming: From Problem Analysis to Program Design, Fourth Edition3Control StructuresA computer can proceed:In sequenceSelectively (branch) - making a choiceRepetitively (iteratively) - loopingSome statements are executed only if certain conditions are met A condition is met if it evaluates to trueC++ Programming: From Problem Analysis to Program Design, Fourth Edition4Control Structures (continued)C++ Programming: From Problem Analysis to Program Design, Fourth Edition5Relational OperatorsA condition is represented by a logical (Boolean) expression that can be true or falseRelational operators: Allow comparisonsRequire two operands (binary)Evaluate to true or falseC++ Programming: From Problem Analysis to Program Design, Fourth Edition6Relational Operators (continued)C++ Programming: From Problem Analysis to Program Design, Fourth Edition7Relational Operators and Simple Data TypesYou can use the relational operators with all three simple data types:8 5.8 evaluates to false5.9 = 21) || ( x == 5) //Line 1 (grade == 'A') && (x >= 7) //Line 2C++ Programming: From Problem Analysis to Program Design, Fourth Edition24int Data Type and Logical (Boolean) ExpressionsEarlier versions of C++ did not provide built-in data types that had Boolean valuesLogical expressions evaluate to either 1 or 0The value of a logical expression was stored in a variable of the data type intYou can use the int data type to manipulate logical (Boolean) expressionsC++ Programming: From Problem Analysis to Program Design, Fourth Edition25The bool Data Type and Logical (Boolean) ExpressionsThe data type bool has logical (Boolean) values true and falsebool, true, and false are reserved wordsThe identifier true has the value 1 The identifier false has the value 0C++ Programming: From Problem Analysis to Program Design, Fourth Edition26Logical (Boolean) ExpressionsLogical expressions can be unpredictableThe following expression appears to represent a comparison of 0, num, and 10: 0 18){ cout to disable the assert statementC++ Programming: From Problem Analysis to Program Design, Fourth Edition55Programming Example: Cable Company BillingThis programming example calculates a customer’s bill for a local cable company There are two types of customers: ResidentialBusinessTwo rates for calculating a cable bill: One for residential customersOne for business customersC++ Programming: From Problem Analysis to Program Design, Fourth Edition56Programming Example: RatesFor residential customer:Bill processing fee: $4.50Basic service fee: $20.50Premium channel: $7.50 per channelFor business customer:Bill processing fee: $15.00Basic service fee: $75.00 for first 10 connections and $5.00 for each additional connectionPremium channel cost: $50.00 per channel for any number of connectionsC++ Programming: From Problem Analysis to Program Design, Fourth Edition57Programming Example: RequirementsAsk user for account number and customer codeAssume R or r stands for residential customer and B or b stands for business customerC++ Programming: From Problem Analysis to Program Design, Fourth Edition58Programming Example: Input and OutputInput: Customer account numberCustomer codeNumber of premium channelsFor business customers, number of basic service connectionsOutput: Customer’s account numberBilling amountC++ Programming: From Problem Analysis to Program Design, Fourth Edition59Programming Example: Program AnalysisPurpose: calculate and print billing amountCalculating billing amount requires:Customer for whom the billing amount is calculated (residential or business)Number of premium channels to which the customer subscribesFor a business customer, you need: Number of basic service connectionsNumber of premium channelsC++ Programming: From Problem Analysis to Program Design, Fourth Edition60Programming Example: Program Analysis (continued)Data needed to calculate the bill, such as bill processing fees and the cost of a premium channel, are known quantitiesThe program should print the billing amount to two decimal placesC++ Programming: From Problem Analysis to Program Design, Fourth Edition61Programming Example: Algorithm DesignSet precision to two decimal placesPrompt user for account number and customer typeIf customer type is R or rPrompt user for number of premium channelsCompute and print the billIf customer type is B or bPrompt user for number of basic service connections and number of premium channelsCompute and print the billC++ Programming: From Problem Analysis to Program Design, Fourth Edition62Programming Example: Variables and Named ConstantsC++ Programming: From Problem Analysis to Program Design, Fourth Edition63Programming Example: FormulasBilling for residential customers:amountDue = RES_BILL_PROC_FEES + RES_BASIC_SERV_COST + numOfPremChannels * RES_COST_PREM_CHANNEL;C++ Programming: From Problem Analysis to Program Design, Fourth Edition64Programming Example: Formulas (continued)Billing for business customers:if (numOfBasicServConn , >=, !=Logical expressions evaluate to 1 (true) or 0 (false)Logical operators: ! (not), && (and), || (or)C++ Programming: From Problem Analysis to Program Design, Fourth Edition70Summary (continued)Two selection structures: one-way selection and two-way selectionThe expression in an if or if...else structure is usually a logical expressionNo stand-alone else statement in C++Every else has a related ifA sequence of statements enclosed between braces, { and }, is called a compound statement or block of statementsC++ Programming: From Problem Analysis to Program Design, Fourth Edition71Summary (continued)Using assignment in place of the equality operator creates a semantic errorswitch structure handles multiway selectionbreak statement ends switch statementUse assert to terminate a program if certain conditions are not met

Các file đính kèm theo tài liệu này:

  • ppt9781423902096_ppt_ch04_9271.ppt
Tài liệu liên quan