You are on page 1of 3

Programming in Pascal- Lesson # 2

Conditional Statements/ IF Constructs

IF-THEN construct

Syntax:

If (condition) then (used when only ONE statement is to be executed if the condition is TRUE)
Statement1;

OR

If (condition) then (used when MORE THAN ONE statement is to be executed if the condition is TRUE)
begin
Statement1;
Statement2;
end;

NB: ‘Begin’ and ‘end’ MUST be used to enclose multiple statements to be executed based on a
condition

Example:
If (num < 10) then
Writeln(‘the number is small’);

OR
If (num < 10) then
begin
Writeln (‘the number is small’);
Squ := (num * num);
Writeln (‘The square is’, squ);
end;

Question1: Write a Pascal program to read the amount to be withdrawn from a bank account. The maximum limit
is $1000. The program should print the statement ‘Charge accepted’, if the amount requested does not exceed
the limit.

Question2: Write a Pascal program to read the name and grade of a student. If the grade exceeds 80 print the
name of the student and the word ‘PASS’.

IF-THEN-ELSE construct

If (condition) then (used when only ONE statement is to be executed if the condition is TRUE)
Statement1 (NO semicolon a this statement because it’s NOT the end of the IF construct)
Else
Statement1; (Semicolon MUST be at this statement)

OR
If (condition) then (used when MORE THAN ONE statement is to be executed if the condition is TRUE)
begin
Statement1; (Semicolon MUST be at this statement)
Statement2; (Semicolon MUST be at this statement)
Statement3; (Semicolon MUST be at this statement)
end (NO semicolon at this statement because it is NOT the end of the IF construct)
else
begin
Statement1; (Semicolon MUST be at this statement)
Statement2; (Semicolon MUST be at this statement)
Statement3; (Semicolon MUST be at this statement)
end; (Semicolon MUST be at this statement because it is the end of the IF construct)

NB: Semicolons are NEVER placed at a line of code that comes DIRECTLY above an ELSE

Question1: Write a Pascal program to read the amount to be withdrawn from a bank account. The maximum limit
is $1000. If the amount requested exceeds the limit the program should print the statement ‘You have exceeded
the limit’, otherwise it should calculate and print the difference.

Question 2: Write a Pascal program to input the maximum and m minimum temperature of a day. If the minimum
temperature is less than 30 degrees centigrade, output the message “It is a cold day” otherwise find the average
temperature for that day and output it with a suitable label.

IF-THEN-ELSE-IF constructs

If (condition) then (used when only ONE statement is to be executed if the condition is TRUE)
Statement1 (NO semicolon a this statement because it’s NOT the end of the IF construct)
Else
If (condition) then
Statement1 (NO semicolon a this statement because it’s NOT the end of the IF construct)
Else
If (condition) then
Statement1; (Semicolon MUST be at this statement because it is the end of the IF construct)

OR
If (condition) then (used when MORE THAN ONE statement is to be executed if the condition is TRUE)
begin
Statement1; (Semicolon MUST be at this statement)
Statement2; (Semicolon MUST be at this statement)
Statement3; (Semicolon MUST be at this statement)
end (NO semicolon at this statement because it is NOT the end of the IF construct)
else
If (condition) then
begin
Statement1; (Semicolon MUST be at this statement)
Statement2; (Semicolon MUST be at this statement)
Statement3; (Semicolon MUST be at this statement)
end (NO semicolon at this statement because it is NOT the end of the IF construct)
Else
If (condition) then
begin
Statement1; (Semicolon MUST be at this statement)
Statement2; (Semicolon MUST be at this statement)
Statement3; (Semicolon MUST be at this statement)
end; (Semicolon MUST be at this statement because it is the end of the IF construct)

NB: Semicolons are NEVER placed at a line of code that comes DIRECTLY above an ELSE

Question 1: A stadium has three stands: A, B and C. The admission fee for stand A is $2.00, stand B is
$2.50, and stand C is $4.00. Write a Pascal program to read a stand and the number of spectators in the
stand. Calculate and print the Revenue collected and the stand entered.

You might also like