You are on page 1of 12

Selection Control

Structures/Selection
Statements/Conditional
Branching
If-THEN-ELSE
Standard
• EK 4.1.2G Every algorithm can be constructed using only sequencing,
selection and iteration

There are three control


structures
Control Structures
• Sequential means each line is executed line by line
• Selection means the program will do something based on a condition
• Iteration means a command is repeated multiple times
Let’s Recap
• In lesson 7 we learnt that programs can be used to perform actions based on a given condition.
• We were introduced to IF-THEN constructs.
• In IF THEN constructs A thing may be done if the condition is true

• For example:
PRINT “Enter the price”
INPUT price
IF price > 60 THEN
dis=10/100*price
Newprice=price-dis
PRINT “The new price is”, Newprice
ENDIF
Recap Cont’d
• Noticed that the 10% discount will only be given if the price is greater
than 60.
Key Points
• In this lesson we will examine IF-THEN- ELSE construct.
• With this construct something may happen if a condition is achieved and another thing
may occur if the same condition is not achieved.
• The format for IF-THEN-ELSE is:

• IF (condition) THEN
action
• ELSE
another action ;
End;
Readln();
END.
Let’s examine the Construct closer.
• Write an algorithm that accepts a number. If the number is 5 print
“Yes” otherwise print “No”.

I P O
num 1) PRINT “Enter a Yes or No
number”
2) INPUT num
3) IF num=5 THEN
4) PRINT “Yes”
5) ELSE
6) PRINT “No”
7) ENDIF
Pseudocode
START
DECLARE num as INTEGER
PRINT “Enter a number”
INPUT num
IF num=5 THEN
PRINT “Yes”
ELSE
PRINT “No”
ENDIF
STOP
Flowchart
START

INPUT num

If Yes
No
num=
5

PRINT “No” PRINT “Yes”

STOP
Pascal
Program Numero;
VAR
Num:integer;
BEGIN
Writeln(‘Program is designed to accept the number 5 and print yes’);
Writeln(‘Please enter a num’);
Read(num);
If (num=5) then
BEGIN
Writeln(‘Yes’)
Else
Writeln(‘No’);
End;
Readln;
END.
Conclusion
• Two types of Selection is IF-THEN and IF-THEN-ELSE
• IF-THEN is used when there is only ONE possible outcome if a
condition met
• IF-THEN-ELSE is used one of two things may occur based on the
condition in a program.
Let’s look at these questions;
1.Write an algorithm in pseudocode,flowchart , pascal program and to
implement the program in Dev Pascal that will accept the age of a
person and display the message “You Can Now Get A Driver’s
Licenses”, If you are over 17 otherwise display “You Are Not Old
Enough To Drive”.

2. Write an algorithm in pseudocode,flowchart , pascal program and to


implement the program in Dev Pascal that will enter a letter of the
alphabet. If the letter is A, E, I, O, or U, display “These Are Vowels”. If
not display “The Letter Is a Consonant”.

You might also like