Selection
▪ The expression which result on TRUE or
FALSE.
▪ Consists 2 keywords:
▪ if
▪ else
Selection
▪ The selection control structure can be
categorized into 4 groups :
1. if statement
2. if-else statement
3. if-else statement (nested)
4. Switch statement
IF Statement
▪ The IF statement will perform an action if
the condition is true and ignore the
action if the condition is false.
Example : IF Statement
if speed > 110 print “penalty”
Answer : Algorithm
1. Read speed
2. If speed more than 110, print “penalty”
Answer: Pseudo Code
Begin
Read speed
if speed> 110, print “penalty”
end if
End
Answer: Flow Chart
Begin
Read speed
Speed True Print
> 110 “Penalty”
False
End
EXERCISE..
▪ If marks < 40 print “fail”.
IF-ELSE Statement
▪ The IF statement will perform an action if the
condition is true and perform another action if
the condition is false.
Example: if-else statement
If speed > 110 print “penalty”
Else print “no penalty”
Answer : Algorithm
1. Read speed
2. If speed more than 110, print “penalty”
3. Else, print “no penalty”
Answer : Pseudo Code
Begin
Read speed
if speed> 110, print “penalty”
else print “no penalty”
end if
End
Answer : Flow Chart
Start
Read speed
Print “No False Speed True Print
Penalty” > 110 “Penalty”
End
EXERCISE..
If marks < 40 print “fail”
Else print “pass”
IF-ELSE Statement
(Nested)
▪ The IF-ELSE nested statement is a condition
when we have an ‘if’ in another ‘if’ body.
▪ We use this control structure if we have many
selection to be handle with.
▪ Syntax:
if ….
else if …
else …
Example : If-Else Nested
Statement
Create a program that can recognize and print
the price base on user prompt code. If user
enter a non-available value, the program will
print “Code Not Recognize”.
Code Price (RM)
1 2.00
2 4.00
3 6.00
4 8.00
Example: Algorithm
1.Read code
2. If code equal to one, then print “RM2.00”
3. Else if code equal to 2, then print “RM4.00”
4. Else if code equal to 3, then print “RM6.00”
5. Else if code equal to 4, then print “RM8.00”
6. Else print “Code Not Recognize”
Example: Pseudo Code
Begin
Read code
if code = 1
print “RM2.00”
else if code = 2
print “RM4.00”
else if code = 3
print “RM6.00”
else if code = 4
print “RM8.00”
else
Print “Code Not Recognized”
End
Example: Flow Chart
Start Print “Code
Not
Read
code Recognized”
F
Code F Code F Code F Code
=1 =2 =3 =4
T T T T
Print Print Print Print
“RM2.00” “RM4.00” “RM6.00” “RM8.00”
End
EXERCISE..
Develop a program which can determine
a driver expertise by count their training
day.
Training day Expertise
0 None
1-3 Weak
4 - 10 Average
>10 Expert
Switch Statement
Limitation Keyword
Can only Add Your Text
use for 2
data type, Switch
integer and break
character default
ONLY.
Switch Statement
switch ( Variable) void main ( )
{ case <caseValue> : {
Statement1; int Num;
Statement2; cin>>Num;
- switch (Num)
StatementN;
break ; { case 1 :
case <CaseValue> : cout<<”You choose 1.”;
statement1; cout<<Thank you!”;
statement2; break ;
break;
- case 2 :
- cout<<” You choose 2.”;
default : break ;
statement1
statement2; default :
} cout<< “ choose 1 or 2 only.”;
cout<< “understand!);
}
}
When user choose 1, system will display “You choose 1”,” Thank you”.
When user choose 2, system will display “You choose 2”
When user choose 88, system will display “choose 1 or 2 only”.”
understand!”.
#include <iostream> EXAMPLE 1
using namespace std;
int main ()
{
// local variable declaration:
char grade = 'D';
switch(grade)
{
case 'A' :
cout << "Excellent!" << endl;
break;
case 'B' :
case 'C' :
cout << "Well done" << endl;
break;
case 'D' :
cout << "You passed" << endl;
break;
case 'F' :
cout << "Better try again" << endl;
break;
default :
cout << "Invalid grade" << endl;
}
cout << "Your grade is " << grade << endl;
return 0;
}
REPETITION
Repetition
▪ Using loop structure.
▪ 3 types of loop:
▪ While
▪ Do .. While
▪ For
‘While’ Loop
▪ Repeat as pre-condition is true
while (condition)
{
loop body
}
‘While’ Loop
▪ The while construct consists of a block of code
and a condition.
▪ The condition is evaluated, and if the condition
is true, the code within the block is executed.
▪ This repeats until the condition becomes false.
▪ Because while loops check the condition
before the block is executed, the control
structure is often also known as a pre-test
loop.
‘While’ General
Condition True
Loop body
False
‘While’ Example
▪ Create a program that can print the value
which is less than 5.
‘While’ Pseudo Code
Begin
Read value= 0
while value < 5
Print value
value=value+1
end while
End
‘While’ Flow Chart
Start
Value = 0
Value =
Value + 1
Value < 5 ? True
False Print Value
End
‘While’ Source Code
‘Do-While’ Loop
⦿ Repeat as post-condition is true
do
{
loop body
}
while (condition)
‘Do-While’ Loop
▪ The do while construct consists of a block of
code and a condition.
▪ First, the code within the block is executed, and
then the condition is evaluated.
▪ If the condition is true the code within the block
is executed again. This repeats until the
condition becomes false.
▪ Because do while loops check the condition after
the block is executed, the control structure is
often also known as a post-test loop.
‘Do-While’ General
Loop body True
Condition
False
‘Do-While’ Example
▪ Create a program that can print the value which
is less than 5.
‘Do-While’ Pseudo code
Begin
Read value= 0
repeat
Print value
value=value+1
until value < 5
End
‘Do-While’ Flow Chart
Start
Value = 0
Print value Value = True
Value+1
Value < 5 ?
False
End
‘Do-While’ Source Code
‘For’ loop
▪ Suitable if we know the number of iterations.
for (expr1;expr2;expr3)
{
s1;
s2 ;
}
▪ expr1 is executed only once before looping.
▪ expr2 is a Boolean expression. If not given, it is
assumed to be true.
▪ If expr2 is false, the loop is terminated.
▪ After execution of the repeat section, expr3 is
executed.
‘For’ loop
▪ The for loop is often distinguished by an explicit
loop counter or loop variable.
▪ This allows the body of the for loop (the code
that is being repeatedly executed) to know
about the sequencing of each iteration.
▪ for loops are also typically used when the
number of iterations is known before entering
the loop.
‘For’ loop
1
2
5
for (i = 0, i < 5, i++)
p{ rintf(“the3numbers are %d 4 i);
\n”,
}
‘For’ Example
▪ Create a program that can print the value which
is less than 5.
‘For’ Pseudo code
Begin
Read value
For value < 5
Print value
value=value+1
End for
End
‘For’ Flow chart
Start
Value = 0
Value True
<5
Print value value ++
False
End
‘For’ Example