Professional Documents
Culture Documents
04 - EGC3173 - EGC3113 - Selection Structures
04 - EGC3173 - EGC3113 - Selection Structures
04 - EGC3173 - EGC3113 - Selection Structures
1
Chapter Outline
Section 1
1. The if-else Statement
2. Nested if Statements
Section 2
3. The switch Statement
2
1. The if-else Statement
3
Control Structures
4
The if-else Statement
5
The if-else Statement
• The condition is evaluated to its numerical value:
– A non-zero value is considered to be true
– A zero value is considered to be false
6
Relational Operators
• The condition may be any valid C++ expression
7
Relational Expressions
• Relational expressions are evaluated to a numerical value of 1 or 0 only:
– If the value is 1, the expression is true
– If the value is 0, the expression is false
9
Relational Expressions
• char values are
automatically
coerced to int
values for
comparison purposes
• Strings are compared
on a character by
character basis
– The string with the
first lower character
is considered smaller
10
Logical Operators
• AND (&&): Condition is true only if both expressions are true
• OR (||): Condition is true if either one or both of the expressions is true
• NOT (!): Changes an expression to its opposite state; true becomes false, false becomes
true
• Example:
• 6 * 3 == 36 /2 || 13 < 3 * 3 + 4 && !(6 - 2 < 5)
11
Logical Operators
12
Logical Operators
How 6 * 3 == 36 /2 || 13 < 3 * 3 + 4 && !(6 - 2 < 5) is executed:
1. 6 * 3 == 36 /2 || 13 < 3 * 3 + 4 && !(6 - 2 < 5)
2. 6 * 3 == 36 /2 || 13 < 3 * 3 + 4 && !(4 < 5)
3. 6 * 3 == 36 /2 || 13 < 3 * 3 + 4 && !(True)
4. 6 * 3 == 36 /2 || 13 < 3 * 3 + 4 && False
5. 18 == 36 /2 || 13 < 3 * 3 + 4 && False
6. 18 == 18 || 13 < 3 * 3 + 4 && False
7. 18 == 18 || 13 < 9 + 4 && False
8. 18 == 18 || 13 < 13 && False
9. 18 == 18 || False && False
10. True || False && False
11. True || False
13
The if-else Statement
Figure 4.2
The if-else flowchart
14
The if-else Statement
• One-way selection: An if
statement without the
optional else portion
15
The if-else Statement
16
Compound Statements
• Compound statement: A sequence of single statements contained between braces
– Creates a block of statements
– A block of statements can be used anywhere that a single statement is legal
– Any variable declared within a block is usable only within that block
17
Compound Statements
//continue…
18
2. Nested if Statements
19
Nested if Statements
• if-else statement can contain any valid C++ statement, including
another if-else
• Nested if statement: an if-else statement completely contained
within another if-else
• Use braces to block code, especially when inner if statement does not
have its own else
20
Nested if Statements
• Syntax:
– if (expression_1)
– {
– if (expression_2)
– statement1;
– else
– statement2;
– }
– else
– statement3;
21
The if-else Chain
• Nested within the else part:
– if (expression_1)
– statement1;
– else
– {
– if (expression_2)
– statement2;
– else
– statement3;
– }
22
The if-else Chain
• if-else chain: A nested if statement occurring in the else clause of the outer
if-else
• If any condition is true, the corresponding statement is executed and the chain
terminates
• Final else is only executed if no conditions were true
– Serves as a catch-all case
• if-else chain provides one selection from many possible alternatives
23
The if-else Chain (continued)
• General form of an if-else chain
24
Common Problems
• Common problems with if-else statements:
– Misunderstanding what an expression is
– Using the assignment operator (=) instead of the relational operator (==)
• Examples:
– Determine the differences:
• age = 30;
• age == 40;
25
3. The switch statement
26
The switch Statement
switch (expression)
• switch statement: Provides for one selection {
from many alternatives case value_1:
…
break;
case value_2:
…
break;
…
case value_n:
…
break;
default:
…
}
27
The switch Statement
switch (expression)
• switch keyword starts the statement {
– Is followed by the expression to be evaluated Start here if case value_1:
expression equals …
to value_1 break;
• case keyword identifies a value to be
Start here if
case value_2:
compared to the switch expression expression equals …
– When a match is found, statements in this case to value_2 break;
block are executed …
case value_n:
…
• All further cases after a match is found are break;
executed unless a break statement is found default:
…
}
28
The switch Statement
switch (expression)
• default case is executed if no other case value {
matches were found Start here if case value_1:
• default case is optional expression equals …
to value_1 break;
Start here if
case value_2:
expression equals …
to value_2 break;
…
case value_n:
…
break;
Start here if none
of previous is default:
matched …
}
29
The switch Statement
30
Summary
31
Summary
Section 1
1. The if-else Statement
2. Nested if Statements
Section 2
3. The switch Statement
32
Concerns?
33
012-4579063
34