IF THEN --ELSE-- Command Syntax in Pascal
Simple Conditions
Complex Conditions, Operators AND and OR
Example of Advanced Mathematical Formula
What Is a Condition
In a real life we often encounter conditions – deciding on the basis on some experience; for example:
If it is raining, I will take an umbrella.
I can only walk across the road when the light is green.
I can only walk across the road when the traffic light is green and there is no car around.
In general: When a condition is valid, then ..., otherwise ... Conditions may be of different levels of complexity and quantity – and this complicates the whole system of conditions. But then the whole system can provide solutions even for relatively complex situations.
IF THEN --ELSE-- Command Syntax in Pascal
In Pascal --as in many other programming languages-- exists the command IF-THEN --ELSE--. The syntax of this command should be:
Basic syntax of the IF-THEN command in Pascal
IF --condition-- THEN command
If we also want to specify what should happen if the condition is not valid, the command IF-THEN includes the part ELSE which is executed if the condition is not valid.
Syntax of the IF-THEN --ELSE-- command in Pascal
IF --condition-- THEN command-1 ELSE command-2
Simple Conditions
Take a look at the following simple example:
IF-THEN in Pascal
Program IsNumberGreaterThanFive;
var a:integer;
begin
readln--a--;
IF --a--5-- THEN writeln--'a is greater than 5'--;
readln;
end.
If we want to have more exact information we use the complete IF-THEN-ELSE commands:
IF-THEN-ELSE in Pascal
Program IsNumberGreaterOrLowerThanFive;
var a:integer;
begin
readln--a--;
IF --a--5-- THEN
writeln--'a is greater than 5'--
ELSE
writeln--'a is not greater than 5'--;
readln;
end.