C++ Programming: From Problem Analysis To Program Design, Fifth Edition
C++ Programming: From Problem Analysis To Program Design, Fifth Edition
C++ Programming: From Problem Analysis To Program Design, Fifth Edition
Objectives
In this chapter, you will: Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean) expressions Discover how to use the selection control structures if, if...else, and switch in a program
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
2
Objectives (contd.)
Learn how to avoid bugs by avoiding partially understood concepts Learn to use the assert function to terminate a program
Control Structures
A computer can proceed:
In sequence Selectively (branch): making a choice Repetitively (iteratively): looping
Some statements are executed only if certain conditions are met A condition is met if it evaluates to true
Relational Operators
A condition is represented by a logical (Boolean) expression that can be true or false Relational operators:
Allow comparisons Require two operands (binary) Evaluate to true or false
Comparing Characters
Expression with relational operators
Depends on machines collating sequence ASCII character set
10
11
12
13
14
15
16
17
Order of Precedence
Relational and logical operators are evaluated from left to right The associativity is left to right Parentheses can override precedence
18
19
20
21
22
You can use the int data type to manipulate logical (Boolean) expressions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
23
24
25
One-Way Selection
The syntax of one-way selection is:
The statement is executed if the value of the expression is true The statement is bypassed if the value is false; program goes to the next statement if is a reserved word
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
26
27
28
29
30
Two-Way Selection
Two-way selection takes the form:
32
33
34
36
37
38
39
40
41
Short-Circuit Evaluation
Short-circuit evaluation: evaluation of a logical expression stops as soon as the value of the expression is known Example:
(age >= 21) || ( x == 5) (grade == 'A') && (x >= 7) //Line 1 //Line 2
42
44
num = 20
45
Can use if statements to check status of input stream If stream enters the fail state, include instructions that stop program execution
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
47
Insert a blank line between statements that are naturally separate Two commonly used styles for placing braces
On a line by themselves Or left brace is placed after the expression, and the right brace is on a line by itself
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
50
switch Structures
switch structure: alternate to if-else switch (integral) expression is evaluated first Value of the expression determines which corresponding action is taken Expression is sometimes called the selector
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
52
53
55
56
The predefined function, assert, is useful in stopping program execution when certain elusive errors occur
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
57
62
Output:
Customers account number Billing amount
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
63
65
If customer type is B or b
Prompt user for number of basic service connections and number of premium channels Compute and print the bill
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
66
67
68
2. 3. 4. 5.
Prompt user to enter account number Get customer account number Prompt user to enter customer code Get customer code
70
71
73
Summary
Control structures alter normal control flow Most common control structures are selection and repetition Relational operators: ==, <, <=, >, >=, != Logical expressions evaluate to 1 (true) or 0 (false) Logical operators: ! (not), && (and), || (or)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
74
Summary (cont'd.)
Two selection structures: one-way selection and two-way selection The expression in an if or if...else structure is usually a logical expression No stand-alone else statement in C++
Every else has a related if
A sequence of statements enclosed between braces, { and }, is called a compound statement or block of statements
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
75
Summary (cont'd.)
Using assignment in place of the equality operator creates a semantic error switch structure handles multiway selection break statement ends switch statement Use assert to terminate a program if certain conditions are not met
76