Chap05 - Selection Control
Chap05 - Selection Control
Chapter 5
Selection Control
Objectives
5.1 Selection Control Structure
5.2 Two Way Selection Structure: if-else Statements
5.2.1 Single-Alternative (Unary) Selection Structure
5.2.2 Dual-Alternative (Binary) Selection Structure
5.2.3 Compound Statements
5.2.4 Conditional Expression (?:)
5.3 Nested if Statements
5.4 Multiple Selection Structure
5.4.1 else-if Statements
5.4.2 switch Statements
5.4.3 Nested switch Statements
Control Structures
Action
true
Action Action
Action
Action
Pseudocode:
200;
else
PYQ:
Convert the following algorithm written in pseudocode
into a complete flowchart and program.
Start
Read sales
If sales >= 5000.00 then
Set rate = 0.20
Else
Set rate = 0.10
End If
Set comm = sales * rate
Display comm
End
PYQ:
Flowchart:
PYQ:
Program: #include <iostream>
using namespace std;
void main()
{
double sales, rate, comm;
cout << “Enter sales: ”;
cin >> sales;
if (sales >= 5000.00)
rate = 0.20;
else
rate = 0.10;
comm = sales * rate
cout << “Commission: ” << comm;
}
Compound (Block of) Statement
• Compound statement (block of statements):
{
statement 1;
statement 2; More than 1 related
. statements to be
. executed as a group
.
statement n;
}
discount=0.1 discount=0.2
Nested if - Example
• TAR Computers gives discount to customers
(members and non-members) when they are buying
computer hardware based on the following table:
Customer Type Discount
Given
Member 10%
Non-member No discount
if (ismember == ‘Y’)
discount_rate = 0.1;
else
discount_rate = 0;
Syntax:
if (condition 1)
statement 1;
else if (condition 2)
statement 2;
…
else if (condition n)
statement n;
else
statement default;
Multiple Selection – else if
- Example
• Write an else-if statement to test if an integer is
positive, negative or zero.
if (integer > 0)
cout<< integer << “ is positive\n";
else if (integer < 0)
cout<< integer << “ is negative\n";
else
cout<< integer << “ is zero\n";
Flowcha
rt
C++ false integer true
statements >0
• Flowchart:
letter
?
‘A’ ‘B’ ‘C’ default
cout<<“Female”;
break;
default:
Multiple Selection - Comparison
Multiple Selection: Switch statement:
if (a == 1) switch (a)
b=1; {
else if (a == 2) case 1:
b=3; b=1;
else break;
b=5; case 2:
b=3;
break;
default:
b=5;
}
Multiple Selection - Comparison
Switch statement:
Multiple Selection: switch (num)
{
case 1:
if (num == 1) a=10;
a = 10; break;
else if (num == 2 || num ==8) case 2:
a = 20; case 8:
else if (num >=4 && num <= 6) a=20;
a = 30; break;
else case 4:
a = 40; case 5:
case 6:
a=30;
break;
default:
a=40;
}
Multiple Selection - Comparison
switch (class){ if (class==’1’)
case '1': classA += 1; {
classA += 1;
case '2': goodClass += 1; goodClass += 1;
break; }
else if (class ==’2’)
case '3': goodClass += 1;
if (average > 3.0) else if (class ==’3’)
classB1 += 1; {
else if (average > 3.0)
classB2 += 1; classB1 += 1;
totalclassB += classB1 else
+ classB2; classB2 += 1;
break; totalclassB += classB1 + classB2;
}
default: poorClass += 1; else
} poorClass += 1;
Nested Switch
A switch statement is nested switch (a)
within another switch. { case 1:
nested if: if (b == 10)
cout<<“Yes”;
if ( a == 1) else if (b == 20)
if (b == 10) cout<<“Yes
cout<<“Yes”; Yes”;
else if (b == 20) break;
cout<<“Yes Yes”; case 2:
else if (a == 2) if (b == 10)
if (b == 10) cout<<“No”;
cout<<“No”; else if (b == 20)
else if (b == 20) cout<<“No
cout<<“No No”; No”;
break;
Nested Switch switch (a)
{ case 1:
switch (b)
switch (a)
{ case 10:
{ case 1:
cout<<“Yes”;
if (b == 10)
break;
cout<<“Yes”;
case 20:
else if (b == 20)
cout<<“Yes
cout<<“Yes Yes”;
Yes”;
}
break;
case 2: break;
if (b == 10) case 2:
cout<<“No”; if (b == 10)
else if (b == 20) cout<<“No”;
cout<<“No else if (b == 20)
No”;
cout <<“No
break; No”;
Nested Switch - Example
• A manufacturer sells machinery parts to its
customers. The price of machinery parts for different
model of machine is listed below:
Model No. Part No. Price (RM)
100 300 1500.00
600 3000.00
200 400 2500.00
800 4000.00
• Use switch statements to get the price of a
particular machinery part based on the model
number and part number. Display an error message
if the model number or part number is unmatched.
Nested Switch - Example
switch (model) case 200:
{ switch (part)
case 100: {
switch (part) case 400: price = 2500.00;
{ break;
case 300: price = 1500.00; case 800: price = 4000.00;
break; break;
case 600: price = 3000.00; default:
break; cout<<"Invalid part num";
default: }
cout<<"Invalid part num"; break;
} default:
break; cout<<“Invalid model num";
}
Nested Switch - Exercise
• Assume that the cost of a product manufactured in year
2016 and year 2019 is 200.50 and 350.30 respectively
without considering the product code. The cost for year
2017 or year 2018 is determined by code as shown in the
table below.
Year Code Cost(RM)
2016 200.50
2017 or A 89.00
2018 B 105.90
Others 79.80
2019 350.30
• Write a program that using nested switch to display the
tuition fee based on user input of year and code.
Nested Switch - Exercise
C++ program:
Summary:
• Single Alternative selection:
if (a != 5) False
a!=5
True
b = 10;
b = 10
else if (a == 1)
False True b = 10
b = 20; a >= b = 20
7
else if (a >= 7)
b = 30; b = 40 b = 30
else
b=40;
Summary:
• Switch statement:
switch (num) num
{ =?
case 1:
a= 1 2 default
10; a = 10 a = 20 a = 30
break;
case 2:
a=
20;
break;
default:
Summary:
• Nested-switch statement:
switch (num) num
{ =?
case 1: a = 10;
break;
case 2: switch(digit) 1 2 default
{ a = 10 digit a = 30
case 11: =?
a=
20;
11 33
break;
a = 20 a = 40
case 33:
a=
40;
}
break;