Topic 4 - Selection Control Structure
Topic 4 - Selection Control Structure
PROGRAMMING 1
LECTURE: 04 – SELECTION CONTROL STRUCTURE
2
Learning Objectives:
After completing this chapter, you will be able to:
• describe about control structures
• use relational and logical operators
• form and evaluate logical (Boolean) expressions
• use the selection control structures if, if...else, and switch
in a program
Introduction (Recap)
• We learnt that an algorithm describes the steps
and the order the steps are arranged to solve a
problem.
crawl
sit
walk
run
walk
yes
obstacle stop
no
run
– Allow comparisons
– Require two operands (binary)
– Return 1 if expression is true, 0 otherwise
Comparing values of different data types may produce unpredictable
results
• Example:
or
no yes
raining?
take an
Umbrella
One-Way (if) Selection
One-Way (if) Selection
One-Way (if) Selection
testScore true
greater than or
equal to 80
cout<<“Thank You”;
One-Way (if) Selection
int testScore;
cout<<“Thank You”;
Two-Way Selection
• Algorithm example, while cooking you may decide
the following:
if we have brown sugar then
use brownSugar
else
use whiteSugar
• Similarly, a payroll program might include a
statement such as:
if hoursWorked is more than 40 then
calculate regularPay and overtimePay
else
calculate regularPay
Two-Way (if…else) Selection
Two-way selection takes the form:
if (expression)
statement1
else
statement2
If expression is true, statement1 is executed otherwise statement2
is executed
else
This statement is executed
cout<<“Work harder" ); if the testScore is less than
80.
Two-Way (if…else) Selection
Two-Way (if…else) Selection
Compound Statements
Use braces if the <then> or <else> block has multiple
statements.
if (testScore < 70)
{
cout<<"You did not pass“ << endl;
cout<<“Try harder next time“ ; Then Block
}
else
{
cout<<“You did pass“ << endl; Else Block
cout<<“Keep up the good work“;
}
Style Guide
if ( <boolean expression> ) {
…
}
else { Style 1
…
}
if ( <boolean expression> )
{
…
} Style 2
else
{
…
}
Nested-if Statement
The then and else block of an if statement can contain any valid statements,
including other if statements. An if statement containing another if
statement is called a nested-if statement.
switch (gradeLevel) {
This statement
is executed if th
case 1: cout<<"Go to the Gymnasium"; e gradeLevel is
break; equal to 1.
Arithmetic Expression
switch ( gradeLevel ) {
Case Label case 1: cout<<"Go to the Gymnasium";
break;
Case
case 2: cout<<"Go to the Science Auditorium";
break;
Body
case 3: cout<<"Go to Dewan Aspirasi”;
break;
case 4: cout<<"Go to Dewan Cemara";
break;
}
switch With No break Statements
true
N equal
x = 10;
switch ( N ) { to 1?
}
false
true
N equal x = 30;
to 3?
false
switch With break Statements
true
N equal
switch ( N ) { to 1?
x = 10;
case 1: x = 10;
false break;
break; true
N equal
case 2: x = 20; to 2?
x = 20;
break;
false break;
case 3: x = 30;
true
break; N equal
to 3? x = 30;
}
false break;
switch With the default Block
switch (ranking) {
case 10:
case 9:
case 8: cout<<"Master";
break;
case 7:
case 6: cout<<"Journeyman";
break;
case 5:
case 4: cout<<"Apprentice";
break;