Last updated on October 8th, 2023 at 03:11 pm
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.
Algorithm Height
Integer height = 0
Start
- Display: Please enter your height in feet;
- Read: height;
- 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
Step | height | height>9 | Output |
---|---|---|---|
1 | Please enter your height in feet. | ||
2 | 10 | ||
3 | True | ||
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
Algorithm printHeight
start
- height = 0
- Display: Please enter your height;
- Read: height;
- if(height>=9)
- a. Display: You must be joking. The world’s tallest person is reportedly less than 9 feet tall.
- Else
- a. Display: height
stop.
Test Data
height = 7
Steps | height | height>=9 | Output | |
---|---|---|---|---|
1 | 0 | |||
2 | Please enter your height. | |||
3 | 7 | |||
4 | false | |||
5a | 7 |
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.
Algorithm tallest
start
- real height1 = 0
- real height2 = 0
- Display: Please enter the first height
- Read: height1
- Display: Please enter the second height
- Read height2
- 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.
- 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.
- Else if(height1>height2)
- a. Display: The tallest person is height1
- Else if(height2>height1)
- a. Display: The tallest person is height2
- 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
Step | height1 | height2 | height1>9 | height2>9 | height1>height2 | Output |
---|---|---|---|---|---|---|
1 | 0 | |||||
2 | 0 | |||||
3 | Please enter the first height. | |||||
4 | 8 | |||||
5 | Please enter the second height. | |||||
6 | 6 | |||||
7 | false | |||||
8 | false | |||||
9 | true | |||||
9a | The 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.
- 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.
- 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.