Chapter 3 Decision Structures and Boolean Logic ITP 134 C++ Study Guide Chapter 3 Decision Structures and Boolean Logic Instructions: Use this Study Guide to help you understand the important points of this chapter. See the book for lots of great programming examples. Important concepts, keywords, statements and functions are shown in emphasized font. Code syntax and examples are shown in code font. Games & Graphics in C++ 2nd edition by Tony Gaddis 3.1 Control Structures CONCEPT: Control structures affect the order in which statements execute. There are 3 main types of control structures, sequence, selection, and repetition. (pg 87) 1. Sequence – set of statements that execute in the order that they appear 2. Selection (also called decision) – structure used to create a program with more than one path of execution (pg 89) 3. Repetition (also called loop or iteration ) – structure used to repeat the same set of statements a number of times (pg 89) Combine Control Structures (Not in book, from CSC 200 class) Each control structure has a single entry and a single exit point. Two ways to combine control structures, stack or nest. See Section 3.4 for nested structures. Figure 3.8 on page 102 shows example of stacked structures. Figure 3.9 on page 103 shows example of nested structures. Flowchart Symbols (from CSC 200 class) Terminal symbol. Starts and ends a program. Add Start at the beginning and End at the end. Use the function name in this symbol to start functions. Decision symbol. Used to ask a question. Put the question inside the symbol. Process symbol. Used for statements inside a block or sequence. Put the pseudocode statement inside the symbol. Connector symbol. Used to connect flowcharts on the same page. Put A, B, C etc inside the symbol. Off page symbol. Used to connect flowcharts on 2 pages. Put the page number inside the symbol. Data symbol. Use for input or output. ITP 134 – Mrs. Eaton Chapter 3 Study Guide – 2nd Edition Page 1 Chapter 3 Decision Structures and Boolean Logic /* executes if false */ statements; 3.2 Writing a Decision Structure with the if Statement CONCEPT: The if statement is used to create a decision structure, which allows a program to have more than one path of execution. The if statement causes one or more statement to execute only when a Boolean expression is true. If statement (general form) is: (see page 90) Use for true/false type questions. if (expression) { statement; statement; statement; } In the Spotlight: Auto Repair Payroll Create a program to calculate hourly rate and overtime (pg 100-101) 3.4 Nested Decision Structures and the ifelse-if Statement CONCEPT: To test more than one condition, a decision structure can be nested inside another decision structure. (pg 101) See Figure 3-10 Flowchart for Nested if statements on page 104 for good examples. Boolean Expressions and Relational Operators The value of a Boolean expression can be either true or false. Boolean algebra is named after the English mathematician George Boole. (pg 92) Relational Operators: (See table 3-1 on page 92) } Programming Style and Nested Decision Structure See page 106 at top for wrong way to indent code. See Figure 3-11 Alignment of if and else clauses (pg 106) for to the professional way to indent code. The if-else-if Statement Nested if statements can become complex. C++ provides a special if-else-if statement which makes the logic easier to write. Use as an alternative to nested if statements. (Page 109) Here is the logic: > greater than; < less than >= greater or equal <= less or equal == equal to != not equal if (expression1) { /* executes if true */ statements; } else if (expression2) { //executes expression2 true statements; } // insert more else if clauses else { // executes expression1 false statements; } Putting it All Together See Program 3-1, TestAverage.cpp for an example of if statement program. (pg 96) 3.3 The if-else Statement CONCEPT: An if-else statement will execute one block of statements if its Boolean expression is true, or another block if the Boolean expression is false. If-else statement (general form) is: Use for true/false type questions. (See page 98) if (expression) { /* executes if true */ statements; } else { ITP 134 – Mrs. Eaton 3.5 Logical Operators CONCEPT: The logical AND operator (&&) and the logical OR operator (||) allow you to connect multiple Boolean expressions to create a compound expression. Chapter 3 Study Guide – 2nd Edition Page 2 Chapter 3 Decision Structures and Boolean Logic The logical NOT operator (!) reverses the truth of a Boolean expression. (page 112) Truth Tables for Boolean Expressions Not in Book AND logical operator is true only when both are true. A B A && B F F F F T F T F F T T T OR logical operator false only when both are false. A B A || B F F F F T T T F T T T T NOT logical operator reverses Boolean value A T F !A F T Precedence of Logical Operators NOT has a higher precedence that the relational operators. AND and OR have a lower precedence than the relational operators. (page 115) 3.6 The switch Statement CONCEPT: The switch statement lets the value of a variable or an expression determine which path of execution the program will take. (page 116) The switch statement is used for multiple choice type decisions. See Figure 3-213 (page 117) for a logical view of a switch statement. The general syntax for a switch statement is switch(x) { case value1: // executes when x=value1 statements; break; case value2: // executes when x=value2 statements; break; // insert other case statements default: statements; } 3.7 Bool Variables CONCEPT: A bool (Boolean) variable can store the values true and false. Variables of the bool data type are commonly used as flags which indicate whether specific conditions exist. For example, // flag if more than 5000 pts bool grandMaster; if (points > 5000) { grandMaster = true; } else { grandMaster = false; } 3.8 Comparing Strings CONCEPT: C++ allows you to compare strings. This allows you to create decision structures that test the value of a string. (pg 123) Important points about switch statements (page 118) a) X value must be an integer or an expression that gives an integer value. b) Value that follows the case keyword must be an integer literal or constant. It cannot be a variable. c) The break clause is optional, but if you do not use it the next case clause will also execute. d) The default clause is optional, but it a smart exception handling statement. Chapter 3 Program Examples e) Since default clause it at the end, it does not need a break statement. ITP 134 – Mrs. Eaton Chapter 3 Study Guide – 2nd Edition Program 3-1 TestAverage.cpp (pages 95-97) Program 3-2 AutoRepairPayroll.cpp (page 100101) Program 3-3 LoanQualifier.cpp (page 104-105) Program 3-4 Grader.cpp (page 107-109) Program 3-5 Grader2.cpp (page 110-111) Program 3-6 SwitchExample.cpp (page 119-121) Program 3-7 CompareStrings.cpp (Page 123124) Page 3