Chapter 3 Part 1n2
Chapter 3 Part 1n2
Control Structures
Part 1
Learning outcome
1
27/8/2024
Control Structures (1 of 2)
➢A computer can proceed:
• In sequence
• Selectively (branch): making a choice
• Repetitively: looping
• By calling a function
Control Structures (2 of 2)
2
27/8/2024
Relational expression
operand operand
3
27/8/2024
Relational expressions (1 of 3)
Suppose i = 1, j = 2 and k = 3:
3 >= 8
i != (2 * j – 1) True 1
1 != 3
4
27/8/2024
Relational expressions (2 of 3)
9*1*5
Note: True = 1
Relational expressions (3 of 3)
Assume x is 10, y is 7, and z, a and b are int or bool
Statement Outcome
z=x<y z is assigned 0 because x is not less than y .
cout << (x > y); Displays 1 because x is greater than y.
a = x >= y; a is assigned 1 because x is greater than or equal to y.
cout << (x <= y); Displays 0 because x is not less than or equal to y .
b = y != x; b is assigned 1 because y is not equal to x .
10
5
27/8/2024
Comparing Characters (1 of 2)
In an expression of
character values using
relational operators:
✓ The result depends on
the lexicographic order
(numeric values
assigned to each
character)
✓ ASCII character set
11
Comparing Characters (2 of 2)
Expression Value Interpretation
‘A’ > ‘C’ 0 false
‘D’ <= ‘Z’ 1 true
‘E’ == ‘F’ 0 false
‘g’ >= ‘m’ 0 false
‘b’ != ‘c’ 1 true
‘a’ == ‘A’ 0 false
12
6
27/8/2024
13
14
7
27/8/2024
➢The data type bool has logical (Boolean) values true and false
➢bool, true, and false are reserved words
➢The identifier true has the value 1
➢The identifier false has the value 0
15
16
8
27/8/2024
Expression !(Expression)
true (nonzero) false (0)
false (0) true (1)
18
9
27/8/2024
19
20
10
27/8/2024
Order of Precedence (1 of 3)
Operators Associativity
➢Relational and logical operators
!, ++, -- (unary operators) Right to left
are evaluated from left to right
( ) Right to left
• The associativity is left to right
*, /, % Left to right
➢Parentheses can override
+, - Left to right
precedence <, <=, >=, > Left to right
==, != Left to right
&& Left to right
|| Left to right
=, +=, -=, *=, /= Right to left
21
Order of Precedence (2 of 3)
Suppose you have
the following
declarations:
22
11
27/8/2024
Order of Precedence (3 of 3)
Suppose you have
the following
declarations:
23
Write relational expressions to express the following conditions (using variable names of your choosing):
1. A person’s age is equal to 30.
2. A person’s temperature is greater than 98.6 degrees.
3. A person’s height is less than 6 feet.
4. The letter input is m.
5. A person’s age is equal to 30, and the person is taller than 6 feet.
6. The current day is the 15th day of the 1st month.
7. A person is older than 50 or has been employed at the company for at least 5 years.
8. A person’s identification number is less than 500 and the person is older than 55.
9. A length is greater than 2 feet and less than 3 feet.
24
12
27/8/2024
25
26
13
27/8/2024
28
14
27/8/2024
29
30
15
27/8/2024
Short-Circuit Evaluation
➢Short-circuit evaluation is a programming concept in which the compiler skips the
execution or evaluation of some sub-expressions in a logical expression.
➢The compiler stops evaluating the further sub-expressions as soon as the value of
the expression is determined.
If the expression age >= 21 is true, then
➢Example: x == 5 is not evaluated because the
expression’s result has already been
determined
(age >= 21) || (x == 5)
Similarly, if the expression grade ==
‘A’ is false, then x >= 7 is not
(grade == ‘A’) && (x >= 7) evaluated.
31
32
16
27/8/2024
33
34
17
27/8/2024
35
0 <= num <= 10 = 0 <= 5 <= 10 Relational operators are evaluated from left to right.
= (0 <= 5) <= 10 0 <= 5 will be evaluated and the result is true. The
value true is represented in memory by the number 1
= 1 <= 10 Therefore, the expression 0 <= 5 evaluates to 1.
Next, it compares 1 <= 10
= 1 (true)
18
27/8/2024
SELECTION STRUCTURE
❑ There are three types of selection structures in C++ language:
➢ if
• used for one-way selection
• used when we want a single or a group of statements to be executed if the condition is true
➢ if-else
• used for two-way selection
• the else statement will be executed when the condition is false
➢ switch or if-else-if
• used for multi-way selection
• allow us to select one from multiple options
38
19
27/8/2024
One-Way Selection (1 of 4)
➢One-way selection syntax
(decision maker)
(action statement)
39
One-Way Selection (2 of 4)
40
20
27/8/2024
One-Way Selection (3 of 4)
WARNING!
41
One-Way Selection (4 of 4)
42
21
27/8/2024
2. If the variable price is greater than 500m then assign 0.2 to the variable
discountRate.
43
Two-Way Selection (1 of 4)
➢Two-way selection syntax
44
22
27/8/2024
Two-Way Selection (2 of 4)
45
Two-Way Selection (3 of 4)
In this example,
if...else statement
is used to check whether
a number entered by the
user is even or odd.
46
23
27/8/2024
2. If the variable sales is greater than or equal to 50000.00 then assign 0.20
commissionRate. Otherwise, assign 0.10 to commissionRate.
47
48
24
27/8/2024
49
50
25
27/8/2024
51
Block Scope (1 of 2)
52
26
27/8/2024
Block Scope (2 of 2)
53
54
27
27/8/2024
Control Structures
Part 2
55
Learning Outcome
56
28
27/8/2024
Nested if statements (1 of 3)
57
Nested if statements (2 of 3)
58
29
27/8/2024
Nested if statements (3 of 3)
➢For readability and easier debugging, it’s important to use proper alignment and
indentation in a set of nested if statements.
59
Note: In this example, each of the if Note: In this example, if the first
statements is checked regardless condition is satisfied then the else part
whether the first if condition is true will not be checked.
or not
60
30
27/8/2024
if-else-if statement (1 of 2)
➢The if-else-if
statement tests a series of
conditions. It is often simpler
to test a series of conditions
with the if-else-if
statement than with a set of
nested if-else
statements.
61
if-else-if statement (2 of 2)
Assume that score is a variable of type int. Based on the value of the score, the
following code outputs the grade:
if (testScore >= 90)
cout << "Your grade is A.\n";
else if (testScore >= 80)
cout << "Your grade is B.\n";
else if (testScore >= 70)
cout << "Your grade is C.\n";
else if (testScore >= 60)
cout << "Your grade is D.\n";
else
cout << "Your grade is F.\n";
62
31
27/8/2024
63
The grade level of undergraduate college students is typically determined according to the
following schedule:
Using this information, write a C++ program that accepts the number of credits a student
has completed, determines the student’s grade level, and displays the grade level.
64
32
27/8/2024
Null statements (1 of 3)
➢represented by a single semicolon (;)
➢often used in situations where a statement is required by the syntax, but no
action needs to be taken.
if (condition) {
// Do something
}
else {
; // Null statement
}
65
Null statements (2 of 3)
int x = 10, y = 20;
if (x > y) {
cout << "x is greater than y" << std::endl;
}
else {
; // Null statement
}
66
33
27/8/2024
Null statements (3 of 3)
int choice = 2;
switch (choice) {
case 1: cout << "You selected option 1." << endl;
break;
case 2: ; // Null statement
break;
case 3: cout << "You selected option 3." << endl;
break;
default: cout << "Invalid choice." << endl;
break;
}
67
68
34
27/8/2024
if (x < 0)
y = 10;
else
z = 20;
69
70
35
27/8/2024
71
switch Statements (1 of 4)
➢switch statement is an alternate switch (Expression)
to if...else...if {
case Value1:
➢switch (integral) expression is // place one or more
evaluated first // statements here
case Value2:
➢Value of the expression // place one or more
determines which corresponding // statements here
action is taken // case statements may be
repeated as many // times as
➢Expression is sometimes called necessary
the selector default:
// place one or more
// statements here
}
72
36
27/8/2024
switch Statements (2 of 4)
73
switch Statements (3 of 4)
➢ Expression can be either
• a variable of any of the integer data types (including char)
• an expression whose value is of any of the integer data types
➢One or more statements may follow a case label
➢Braces are not needed to turn multiple statements into a single compound statement
➢When a case value is matched, all statements after it will be executed until a break is
encountered
➢ The expression following the word case must be an integer literal or constant. It cannot
be a variable, and it cannot be an expression such as x < 22 or n == 50
➢The break statement may or may not appear after each statement
➢switch, case, break, and default are reserved words
74
37
27/8/2024
switch Statements (4 of 4)
char choice;
75
76
38
27/8/2024
assert function (1 of 4)
➢It is used to check the conditions that cannot happen unless there is a
bug. So, it is used as a debugging tool since it terminates the program
when the assertion becomes false.
77
assert function (2 of 4)
➢ This function is located in cassert header file
➢ Syntax
78
39
27/8/2024
assert function (3 of 4)
In this example, we have used the
assert function to assert that the
value of even_num should be
even.
79
assert function (4 of 4)
80
40