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;
if-then-else: this means thatifa condition is true,thenexecute the conditional code, orelseexecute another non-conditional piece of code. The syntax forif-then-elseis:
if condition then condtionalcode
else code;
- nested statements: these are applied when multiple conditions must be checked before executing conditional code. In this case, an
ifstatement must be true for the following nestedifstatements to be checked. The syntax for nested statements is:
if condition1 then
if condition2 then code
else nonconditional-code;
casestatements: this means that a variable can be checked against a list to check whether they are equal. It is similar to theif-then-elsestatements. The syntax forcasestatements is:
case (variable) of
option1 : code1;
option1: code2;
...
...
optionn: coden;
end;
case-elsestatements: if none of the case values with which a variable is being compared are equivalent, theelsecode is executed. The syntax forcase-elsestatements is:
case (variable) of
option1 : code1;
option1: code2;
...
optionn: coden;
else code:
end;
- nested-case statements: similar to nested
ifstatements, 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 , meaning the remainder after num is divided by is equal to . If true, the code prints that num is even.
program ifelsestatement;var num:integer;beginnum := 10;if ((num mod 2)=0) thenwriteln('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;beginnum := 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;beginnum1 := 5;num2 := 10;if ((num1 mod 2)=0) thenif ((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;beginmath := 'A';physics := 'B';case (math) of'A': begincase (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':begincase (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':begincase (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.
Free Resources