Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visual C++ Programming: Concepts and Projects

Similar presentations


Presentation on theme: "Visual C++ Programming: Concepts and Projects"— Presentation transcript:

1 Visual C++ Programming: Concepts and Projects
Chapter 4A Selection (Concepts)

2 Objectives 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

3 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 Visual C++ Programming

4 Control Structures Control structures Three types
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

6 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 Visual C++ Programming

7 Visual C++ Programming

8 Visual C++ Programming

9 Selection Control Structures
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

11 Relational Operators 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

14 Using if Statements to Provide a Single Alternative
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

17 Using if…else Statements to Provide Two Alternatives
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

20 Logical Operators 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

21 Logical Operators Operators with Boolean operands
Include operators for “and”, “or” and “not” Logical operators Not (!) And (&&) Or (||) Visual C++ Programming

22 The not Operator (!) 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

25 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 Visual C++ Programming

26 Visual C++ Programming

27 Visual C++ Programming

28 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 Visual C++ Programming

29 Visual C++ Programming

30 Determining When to Use the and operator (&&) and the or operator (||)
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

35 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 Visual C++ Programming

36 Visual C++ Programming

37 Visual C++ Programming

38 Nested Control Structures
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

43 Multiple Alternative Selection
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

50 Summary 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

51 Summary (continued) 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

52 Summary (continued) Boolean expressions often use relational operators
>, >=, <, <=, ==, != Complex Boolean expressions can be evaluated using logical operators and (&&) and or (||) Visual C++ Programming


Download ppt "Visual C++ Programming: Concepts and Projects"

Similar presentations


Ads by Google