530 likes | 534 Views
Discover the importance of control structures, learn about sequential and selection control structures, compare values using relational operators, use if and if...else statements, develop an understanding of logical operators, create nested control structures, and become familiar with multiple selection and the switch statement.
E N D
Programming with Visual C++: Concepts and Projects Chapter 4A: Selection (Concepts)
Objectives In this chapter, you will: 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 Programming with Visual C++: Concepts and Projects
Objectives (continued) 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 Programming with Visual C++: Concepts and Projects
Control Structures • Control structures • Are the fundamental building blocks of all programs • Control the flow of tasks • Some control structures allow you to build decision-making capabilities into programs • Other control structures enable statement repetition • Three types of control structures • Sequential • Selection • Repetition Programming with Visual C++: Concepts and Projects
Control Structures (continued) Programming with Visual C++: Concepts and Projects
Sequential Control Structures • 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 • Example: Figure 4-2 with click event handler code shown in Example 4-1 (following slides) Programming with Visual C++: Concepts and Projects
Sequential Control Structures (continued) Programming with Visual C++: Concepts and Projects
Sequential Control Structures (continued) Programming with Visual C++: Concepts and Projects • Sequential control (3 steps, executed in order) • Declare score • Read score • Display “Pass”
Selection Control Structures • Selection structures build decision-making capabilities into your program by providing one or more alternative courses of action • Single alternative selection • Double alternative selection • Multiple alternative selection • A Boolean expression is evaluated and used to select a course of action Programming with Visual C++: Concepts and Projects
Selection Control Structures (continued) Programming with Visual C++: Concepts and Projects
Relational Operators • Boolean expressions compare two values • For example: score >= 60 • The relationship between two variables is evaluated using the relational operators >, >=, <, <=, ==, != (see table 4-1 on next slide) • If more than one relational operator appears in an expression then those with the highest order of precedence are executed first Programming with Visual C++: Concepts and Projects
Relational Operators (continued) Programming with Visual C++: Concepts and Projects
Relational Operators (continued) All relational operators are binary (have two operands) All expressions involving a relational operator are resolved from left to right (left-to-right associative) The equal to (==) and not equal to (!=) operators have lowest precedence Programming with Visual C++: Concepts and Projects
Using if Statements to Provide a Single Alternative • Syntax: • keyword if • Boolean expression • The Boolean expression must be enclosed in a set of parenthese( ) • If the Boolean expression evaluates to true then one or more statements are executed • Example: • if(score >= 60)txtGrade->Text = “Pass”; Programming with Visual C++: Concepts and Projects
Using if Statements to Provide a Single Alternative (continued) Programming with Visual C++: Concepts and Projects If only one task is to be executed it follows the Boolean expression
Using if Statements to Provide a Single Alternative (continued) Multiple tasks within an if statement are enclosed in a set of curly brackets { } Programming with Visual C++: Concepts and Projects
Using if Statements to Provide a Single Alternative (continued) Programming with Visual C++: Concepts and Projects
Using if…else Statements to Provide Two Alternatives • The keyword else is used to separate 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 Programming with Visual C++: Concepts and Projects
Using if…else Statements to Provide Two Alternatives (continued) Programming with Visual C++: Concepts and Projects
Using if…else Statements to Provide Two Alternatives (continued) Programming with Visual C++: Concepts and Projects
Logical Operators • Logical operators are used to combine two or more relational expressions • Logical operators • Not (!) • And (&&) • Or (||) • Pseudocode examples: • If score is not greater than 60 • If score is greater than 0 and less than 100 • If score is less than 0 or greater than 100 Programming with Visual C++: Concepts and Projects
The not Operator (!) • Reverses a Boolean value • Has highest precedence among logical operators • Example: !(score >= 60) • Assume that score is 45. Then, the relational expression score >= 60 is false • ! reverses the evaluation to true Programming with Visual C++: Concepts and Projects
The not Operator (!) (continued) Programming with Visual C++: Concepts and Projects
The not Operator (!) (continued) Programming with Visual C++: Concepts and Projects • The Boolean expression (score >= 60) is • true (if score is 75) • false (if score is 25) • The not operator (!) reverses that evaluation
The and Operator (&&) • 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 Programming with Visual C++: Concepts and Projects
The and Operator (&&) (continued) Programming with Visual C++: Concepts and Projects
The and Operator (&&) (continued) Programming with Visual C++: Concepts and Projects
The and Operator (&&) (continued) 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 Programming with Visual C++: Concepts and Projects
The and Operator (&&) (continued) Programming with Visual C++: Concepts and Projects There are four possible expression evaluation results using the and operator (&&)
Determining When to Use the and operator (&&) and the or operator (||) • Consider a program with: • Two TextBoxes that must contain integers • ComboBox control to indicate which arithmetic operation to perform Programming with Visual C++: Concepts and Projects
Determining When to Use the and operator (&&) and the or operator (||) (continued) Programming with Visual C++: Concepts and Projects
Determining When to Use the and operator (&&) and the or operator (||) (continued) Programming with Visual C++: Concepts and Projects
Determining When to Use the and operator (&&) and the or operator (||) (continued) Programming with Visual C++: Concepts and Projects
Determining When to Use the and operator (&&) and the or operator (||) (continued) • Possible errors requiring complex expressions to filter out • No data in txtLeft and txtRight • Data in one TextBox but not the other • The TextBoxes have valid data in them but no operation has been selected from the ComboBox • Example: • if ((txtLeft->Text == “”) && (txtRight->Text == “”)) Programming with Visual C++: Concepts and Projects
The or Operator (||) 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 Programming with Visual C++: Concepts and Projects
The or Operator (||) (continued) Programming with Visual C++: Concepts and Projects
The or Operator (||) (continued) Programming with Visual C++: Concepts and Projects If either txtLeft or txtRight are empty then display a MessageBox
The or Operator (||) (continued) Programming with Visual C++: Concepts and Projects
Nested Control Structures • Nested control structures are control structures that are placed inside of one another • Nesting is used to implement multiple alternative selection • Common form of selection structure nesting • Double alternative (if…else) statement within one alternative of another if…else statement Programming with Visual C++: Concepts and Projects
Nested Control Structures (continued) Programming with Visual C++: Concepts and Projects
Nested Control Structures (continued) Programming with Visual C++: Concepts and Projects • The SelectedIndex property of a ComboBox is set to -1 by default or if no selection has been made
Nested Control Structures (continued) Programming with Visual C++: Concepts and Projects
Multiple Alternative Selection If there are many alternatives to be selected from then the nesting of if…else statements gets complicated Multiple levels of nesting are required Programming with Visual C++: Concepts and Projects
Multiple Alternative Selection (continued) Programming with Visual C++: Concepts and Projects
Multiple Alternative Selection (continued) Programming with Visual C++: Concepts and Projects
Multiple Alternative Selection (continued) Multiple alternative selection can be handled in other ways, without nested control structures The if…else if statement is made to accommodate multiple selection without nesting Only one alternative (the first one in which the Boolean expression evaluated to true) is executed Programming with Visual C++: Concepts and Projects
Multiple Alternative Selection (continued) Programming with Visual C++: Concepts and Projects
Multiple Alternative Selection (continued) The switch statement also implements multiple selection Keyword switch is followed by an integral value The integral value is used to determine which of several cases (alternatives) will be executed Each case has statements associated with it Control will transfer out of a case only if a break statement or the end of the switch statement is encountered Programming with Visual C++: Concepts and Projects
Multiple Alternative Selection (continued) Programming with Visual C++: Concepts and Projects Syntax of the switch statement
Multiple Alternative Selection (continued) Programming with Visual C++: Concepts and Projects