What is decision making in Pascal?

Every programming language has a decision making structure that pre-empts conditional code, which can only be executed given that certain conditions are met. Below is a flowchart of what these decision making structures usually look like:

In the Pascal language, the following statements are used to make decisions:

1: if-then: this means that if a condition is true, then execute a conditional code provided in the program. This is similar to the structure shown above in the case of a true condition. The syntax for if-then is:

if condition then code;
  1. if-then-else: this means that if a condition is true, then execute the conditional code, or else execute another non-conditional piece of code. The syntax for if-then-else is:
if condition then condtionalcode
else code;
  1. nested statements: these are applied when multiple conditions must be checked before executing conditional code. In this case, an if statement must be true for the following nested if statements to be checked. The syntax for nested statements is:
if condition1 then
  if condition2 then code

else nonconditional-code;
  1. case statements: this means that a variable can be checked against a list to check whether they are equal. It is similar to the if-then-else statements. The syntax for case statements is:
case (variable) of
   option1 : code1;
   option1: code2;
   ...
   ...
   optionn: coden;
end;
  1. case-else statements: if none of the case values with which a variable is being compared are equivalent, the else code is executed. The syntax for case-else statements is:
case (variable) of
   option1 : code1;
   option1: code2;
   ...
   optionn: coden;
else code:
end;
  1. nested-case statements: similar to nested if statements, nested `case’ statements are when multiple conditions are being checked. In this case, if one variable is equivalent to an option that it is checked against, then we can check another variable. The syntax for nested-case statements is:
case (variable1) of
   option1 : code1;
       case (variable2) of
          option1:code1;
          option2:code2;
          ...
       end;
   option1: code2;
   ...
   ...
   optionn: coden;
end;

Examples

The examples below will help you understand these decisions in more depth.

Note: You can ignore the stdout from Free Pascal Compiler... till ...compiled, 0.0 sec. It is a bug in some versions of LD.

If-then example

The code below defines a variable num. It checks whether num mod 2 is 00, meaning the remainder after num is divided by 22 is equal to 00. If true, the code prints that num is even.

program ifelsestatement;
var num:integer;
begin
num := 10;
if ((num mod 2)=0) then
writeln('num is an even number');
end.

If-then-else example

This is similar to the example above; however, it adds a line in case num is not even. If num is not even, the code prints in the else statement that num is odd.

program ifthenelsestatement;
var num:integer;
begin
num := 5;
if ((num mod 2)=0) then writeln('num is an even number')
else writeln('num is an odd number');
end.

Nested-if example

Nested-if checks for multiple conditions. There are 2 variables: num1 and num2. The code checks that in case num1 is even, then num2 is also even. This is called a nested if statement, where a condition is checked one after another. If both statements are true, only then is the conditional code printed that both the variables are in fact even.

program nestedstatement;
var num1,num2:integer;
begin
num1 := 5;
num2 := 10;
if ((num1 mod 2)=0) then
if ((num2 mod 2)=0) then writeln('both num1 and num2 are even')
else writeln('num1 is even and num2 is odd')
else if ((num2 mod 2)=0) then writeln('num2 is even and num1 is odd')
else writeln('both numbers are odd');
end.

Nested case statement example

The following nested case statement includes everything you must know about case statements. For us to test nested case statements, we must have 2 variables to be checked for equality. By using nested case statements, you first check the grade for math and then physics. Then, you print out a statement for that student’s report card accordingly. Given that the grades of math and physics don’t match any of the options in the case statements, the performance of the student is undetermined.

program nestedcase;
var math,physics: char;
begin
math := 'A';
physics := 'B';
case (math) of
'A': begin
case (physics) of
'A': writeln('Excellent performance in maths and physics');
'B': writeln('Excellent performance in maths and Good performance in physics');
'C': writeln('Excellent performance in maths and Poor performance in physics');
end;
end;
'B':begin
case (physics) of
'A': writeln('Average performance in maths and Excellent performance in physics');
'B': writeln('Average performance in maths and Good performance in physics');
'C': writeln('Average performance in maths and Poor performance in physics');
end;
end;
'C':begin
case (physics) of
'A': writeln('Poor performance in maths and Excellent performance in physics');
'B': writeln('Poor performance in maths and Good performance in physics');
'C': writeln('Poor performance in maths and poor performance in physics');
end;
end;
else writeln('Performance Undetermined');
end;
end.
Copyright ©2024 Educative, Inc. All rights reserved