If Then Else
If Then Else
If-THEN-ELSE
Standard
• EK 4.1.2G Every algorithm can be constructed using only sequencing,
selection and iteration
• 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
• Do something
• ELSE
• Do another something
• ENDIF
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
STOP
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.