Decision Making/Control Structures in Pascal – Lesson 5

Last updated on October 8th, 2023 at 03:11 pm

Grammarly Writing Support

Control structures, conditions, or decision-making in Pascal are necessary to cater to different eventualities. These structures require the program to test and run different outcomes based on the results of those tests. In programming, decision-making is usually indicated by if-then-else or switch statements.

In this lesson, we will explore two algorithms and Pascal programming examples that demonstrate the use of decisions.

If statements

If statements come in several different forms:

  • If: This is a simple statement that executes only if the boolean expression is true.
  • If – then – else: This statement has two different results. One for if the statement is true and the other for if the statement is false.
  • The nested if: The nested if statements can have several different outcomes.

The if statement is related to Boolean operators in Pascal programming. To learn more about this, visit Pascal programming operators.

Algorithm using decision making in Pascal – the simple if statement

For this first example, we will use the problem statement below:

Write a program that accepts the height of the user and prints an error message when the user enters a number above 9 ft.

Flow and Pseudocode Algorithms

In the following section, we have the flow and pseudocode algorithms of the same problem side by side. While it is only necessary to do one or the other, we will do both for many of the problems on this site so that you will be able to practice doing both types. We have our flow chart algorithm on the left and the pseudocode on the right.

Image of the flow chart algorithm for the simple if decision making.

Algorithm Height

Integer height = 0

Start

  1. Display: Please enter your height in feet;
  2. Read: height;
  3. if(height>9)
    • a. Display: Are you sure? The world’s tallest person is reportedly less than 9 feet tall;

Stop.

Test Data:

Height – 10

Pascal programming using the simple if statement

Stepheightheight>9Output
1Please enter your height in feet.
210
3True
3(a)Are you sure? The world’s tallest person is reportedly less than 9 feet tall.

The Pascal program uses decision-making in Pascal

This is the Pascal code of the algorithms above using the simple if statement:

program yourHeight;

var
height:integer;

begin
writeln('Please enter your height in feet:');
readln(height);
if(height>=10)then
writeln('Are you sure? The tallest person in the world is reportedly less than 9 feet tall.');
End.

Most programs use need variables for calculations. Visit Pascal variables to learn more.

Algorithm using the if-then-else decision-making in Pascal

Image of the flow algorithm of the if-then-else decision making.

Algorithm printHeight

start

  1. height = 0
  2. Display: Please enter your height;
  3. Read: height;
  4. if(height>=9)
    • a. Display: You must be joking. The world’s tallest person is reportedly less than 9 feet tall.
  5. Else
    • a. Display: height

stop.

Test Data

height = 7

Stepsheightheight>=9Output
10
2Please enter your height.
37
4false
5a7

Program using the if-then-else decision-making in Pascal

program printHeight;

var
height:real;

begin
height := 0;
writeln('Please enter your height:');
readln(height);

if(height>=9)then
   writeln('You must be joking. The tallest person is reportedly less than 9 feet tall.')
else
   writeln(height:1:2);
end.

Algorithms using the nested if statement

Use the following problem statement to create the algorithms and program demonstrating how the if-then-else statement works.

Write a program that accepts the height of two persons and compares and prints the height of the taller person. If the user enters a height above 9 ft, the program should print an error message.

Image of the nested if decision making of the algorithm on the right.

Algorithm tallest

start

  1. real height1 = 0
  2. real height2 = 0
  3. Display: Please enter the first height
  4. Read: height1
  5. Display: Please enter the second height
  6. Read height2
  7. if (height1>=9)
    • a. Display: Surely, you must be joking. The tallest person in the world is believed to be less than 9 feet tall.
  8. Else if(height2>=9)
    • a. Display: Surely, you must be joking. The tallest person in the world is believed to be less than 9 feet tall.
  9. Else if(height1>height2)
    • a. Display: The tallest person is height1
  10. Else if(height2>height1)
    • a. Display: The tallest person is height2
  11. Else
    • a. Display: These heights are the same.

stop

In programming, arrays, loops, and control structures often work together. To explore while and for loops, visit while and for loop in Pascal programming.

Test data

height1 = 8

height2 = 6

Stepheight1height2height1>9height2>9height1>height2Output
10
20
3Please enter the first height.
48
5Please enter the second height.
66
7false
8false
9true
9aThe tallest person is 8 feet.

Program using the nested if decision-making in Pascal

program tallest;


var
height1 : real;
height2 : real;

begin
writeln('Please enter the first height:');
readln(height1);
writeln('Please enter the second height:');
readln(height2);

if(height1>=9)then
    writeln('You must be joking. The tallest man in the world is believed to be less than 9 feet.')
else if(height2>=9)then
    writeln('You must be joking. The tallest man in the world is believed to be less than 9 feet.')
else if(height1>height2)then
    writeln('The taller person is ', height1:2:2, ' feet.')
else if(height2>height1)then
    writeln('The taller person is ', height2:2:2, ' feet.')
else
    writeln('The heights are the same');
    
end.

Pascal programming exercise

The following are questions that will allow you to practice using control structures – if, if-then-else, and nested-if decision-making in Pascal.

  1. Write a program that accepts as input the age of the user. If the age is under 4, your program should return the information that states that the person should be in kindergarten.
  2. Write a program that accepts as input the age of the user. If the age is under 4, your program should return the information that states that the person should be in kindergarten. If the person is younger than 12, the message should inform the user that the person should be in primary school.

Every programmer needs to learn to debug their program. To learn more about finding and debugging errors in your code, visit our errors on types of errors in programming.

Before you go

We try our best to be as thorough as possible in the information we provide. However, if you have any questions or comments, be sure to leave them below, and we will get back to you in a timely manner.