04 - EGC3173 - EGC3113 - Selection Structures

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Chapter 4: Selection Structures

Muhamad Najib Zamri


Unless otherwise specified, all materials and diagrams are adapted / modified from the slides
accompanying Gary J Bronson’s C++ for Engineers & Scientists 4 th Ed.
SBN-13: 978-1133187844 / ISBN-10: 1133187846

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

• A computer can proceed


– In sequence
– Selectively (branch): making a choice
– Repetitively (iteratively): looping
– Invocation : call function

• Some statements are executed only if certain conditions are met


• A condition is met if it evaluates to true

4
The if-else Statement

• if-else statement: Implements a decision structure for two


alternatives
• Syntax:
if (condition)
statement 1 executed if condition is true;
else
statement 2 executed if condition is false;

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

• The else portion is optional


– Executed only if the condition is false

6
Relational Operators
• The condition may be any valid C++ expression

• Relational expression: Compares two operands or expressions using


relational operators

Table 4.1 C++’s Relational Operators

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

• What is the output of the following statements?


cout << “The value of 35 < 4 is ” << (35 < 4) << endl;
cout << “The value of 35 > 4 is ” << (35 > 4) << endl;
cout << “The value of true is ” << true << endl;
cout << “The value of false is ” << false << endl;

• Is the answer 0,1, true or false?


8
Relational Expressions
• What is the output of the following statements?
cout << “The value of 35 < 4 is ” << (35 < 4) << endl;
cout << “The value of 35 > 4 is ” << (35 > 4) << endl;
cout << “The value of true is ” << true << endl;
cout << “The value of false is ” << false << endl;

• Is the answer 0,1, true or false?


The statement output:
The value of 35 < 4 is 0
The value of 35 > 4 is 1
The value of true is 1
The value of false is 0

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)

• To evaluate the above expression, try This:


• cout << (6 * 3 == 36 /2 || 13 < 3 * 3 + 4 && !(6 - 2 < 5))

11
Logical Operators

Table 4.2 Operator Precedence and Associativity

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

• if-else statement: Performs instructions based on


the result of a comparison
• Place statements on separate lines for readability
• Syntax:

Figure 4.2
The if-else flowchart

14
The if-else Statement

• One-way selection: An if
statement without the
optional else portion

Figure 4.3 Figure 4.2


A one-way selection if statement The if-else flowchart

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;

Figure 4.4a Nested within the if part

21
The if-else Chain
• Nested within the else part:

– if (expression_1)
– statement1;
– else
– {
– if (expression_2)
– statement2;
– else
– statement3;
– }

Figure 4.4b Nested


within the else part

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

You might also like