If Then Else Pascal
If Then Else Pascal
If Then Else Pascal
Structures/Selection
Statements/Conditional
Branching
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
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
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”.