VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 4A Selection (Concepts) Objectives 2 Discover what control structures are and why they are important Learn the difference between sequential control structures and selection control structures Compare values using relational operators Use an if statement to select single alternative Use an if...else statement to select one of two alternatives Visual C++ Programming Objectives (continued) 3 Develop an understanding of logical operators and their use in complex expressions Create nested control structures Become familiar with multiple selection and the switch statement Visual C++ Programming Control Structures 4 Control structures Are the fundamental building blocks of all programs Control the flow of instruction execution Three types Sequential Every statement is executed in sequence Selection Allows you to skip statements Repetition Allows you to repeat statements Visual C++ Programming 5 Visual C++ Programming Sequential Control Structures 6 Linear in nature Each statement is performed in order No statement is skipped No statement is repeated The simplest programs tend to be sequential in nature Visual C++ Programming 7 Visual C++ Programming 8 Visual C++ Programming Selection Control Structures 9 Provide alternative course of action Single alternative selection Double alternative selection Multiple alternative selection A Boolean expression is evaluated and used to select a course of action Visual C++ Programming 10 Visual C++ Programming Relational Operators 11 A Boolean expression often compares two variables The relationship between two variables is evaluated using relational operators The equal to (==) and not equal to (!=) operators have lowest precedence All relational operators are binary (have two operands) All relational operators are left-to-right associative Visual C++ Programming 12 Visual C++ Programming 13 Visual C++ Programming Using if Statements to Provide a Single Alternative 14 Syntax: keyword if followed by a Boolean expression) If a Boolean expression is true then one or more statements are executed If only one task is to be executed it can be listed after the Boolean expression If more than one task is to be executed they must be enclosed in curly brackets { } Visual C++ Programming 15 Visual C++ Programming 16 Visual C++ Programming Using if…else Statements to Provide Two Alternatives 17 The keyword else is used to separate the two alternatives If the Boolean expression is true then the statements in the first alternative are selected If the Boolean expression is false then the statements in the second alternative are selected Visual C++ Programming 18 Visual C++ Programming 19 Visual C++ Programming Logical Operators 20 Operators with Boolean operands Include operators for “and”, “or” and “not” Examples from everyday life. You may have an exam on Monday but want to go to a concert in another city this weekend. If it is true that I have an exam, then I will not try to get concert tickets If it is not true that I have an exam, then I will try to get tickets. Visual C++ Programming Logical Operators 21 Operators with Boolean operands Include operators for “and”, “or” and “not” Logical operators Not (!) And (&&) Or (||) Visual C++ Programming The not Operator (!) 22 Reverses a Boolean value Has highest precedence among logical operators Uses the ! Operator Example: !(score >= 60) Assume that score is 45. Then, the relational expression score >= 60 is false, ! reverses the evaluation to true Visual C++ Programming 23 Visual C++ Programming 24 Visual C++ Programming The and Operator (&&) 25 Used with two Boolean operands – often relational expressions Lower precedence than not (!) Example: if ((score > 0) && (score <= 100)) The operands may both be true The left operand may be true and the right operand false The right operand may be true and the left operand false Visual C++ Programming 26 Visual C++ Programming 27 Visual C++ Programming The and Operator (&&) (continued) 28 If either the left or right operands are false then the entire expression evaluates to false The only way and expression evaluates to true using the and operator (&&) is if both operands are true Visual C++ Programming 29 Visual C++ Programming Determining When to Use the and operator (&&) and the or operator (||) 30 Consider a program with two TextBoxes that must contain integers and a ComboBox control to indicate which arithmetic operation to perform Possible errors of omission No data in both TextBoxes Data in one TextBox but not the other No operation selected from the ComboBox Example: if ((txtLeft->Text == “”) && (txtRight->Text == “”)) Visual C++ Programming 31 Visual C++ Programming 32 Visual C++ Programming 33 Visual C++ Programming 34 Visual C++ Programming The or Operator (||) 35 Unlike the and operator (&&) if either the left or right operands are true then the entire expression evaluates to true The only way and expression evaluates to false using the or operator (||) is if both operands are false Visual C++ Programming 36 Visual C++ Programming 37 Visual C++ Programming Nested Control Structures 38 Nested control structures are ones that are placed inside of another Often used to implement multiple alternative selection A double alternative (if…else) statement within one alternative of another if…else statement Visual C++ Programming 39 Visual C++ Programming 40 Nested Control Structures (continued) Example: a ComboBox can be evaluated to determine whether a selection has been made. Valid ComboBox choices have index values starting at 0 If no selection has been made the SelectedIndex property of a ComboBox is set to -1 by default Visual C++ Programming 41 Visual C++ Programming 42 Visual C++ Programming Multiple Alternative Selection 43 Can be implemented with nested if…else statements The statements work like a filter Whichever Boolean expression evaluates to true first controls that path of execution that the program has The if…else if statement is made to accommodate multiple selection without nesting Visual C++ Programming 44 Visual C++ Programming 45 Visual C++ Programming 46 Visual C++ Programming 47 The switch statement also implements multiple selection Keyword switch is followed by an integral value The integral value is used to determine which case will be executed Each case has statements that will be executed Control will transfer out only it a break statement or the end of the switch statement is encountered Visual C++ Programming 48 Visual C++ Programming 49 Visual C++ Programming Summary 50 Control structures are the building blocks of every computer program Types of control structures • • • Sequential – linear, no statement repeated or skipped Selection – allows one or more statements to be skipped under specific conditions Repetition – the topic of Chapter 5 Visual C++ Programming Summary (continued) 51 Types of selection structures • • • Single alternative (if statement) Double alternative (if…else statement) Multiple alternative Nested if…else statements Multiple alternative if (if…elseif statement) Switch statement All if statements evaluate a Boolean expression Visual C++ Programming Summary (continued) 52 Boolean expressions often use relational operators • >, >=, <, <=, ==, != Complex Boolean expressions can be evaluated using logical operators and (&&) and or (||) Visual C++ Programming