Making Decisions
 

Most programs need to make decisions. There are several statements available in the Pascal language for this. The IF statement is one of the them. The RELATIONAL OPERATORS, listed below, allow the programmer to test various variables against other variables or values.

	=   Equal to
> Greater than
< Less than
<> Not equal to
<= Less than or equal to
>= Greater than or equal to

The format for the IF THEN Pascal statement is,

if condition_is_true then
execute_this_program_statement;

The condition (ie, A < 5 ) is evaluated to see if it's true. When the condition is true, the program statement will be executed. If the condition is not true, then the program statement following the keyword then will be ignored.

program IF_DEMO (input, output); {Program demonstrating IF THEN statement}
var number, guess : integer;
begin
number := 2;
writeln('Guess a number between 1 and 10');
readln( guess );
if number = guess then writeln('You guessed correctly. Good on you!');
if number <> guess then writeln('Sorry, you guessed wrong.')
end.

Executing more than one statement as part of an IF
To execute more than one program statement when an if statement is true, the program statements are grouped using the begin and end keywords. Whether a semi-colon follows the end keyword depends upon what comes after it. When followed by another end or end. then it no semi-colon, eg,


program IF_GROUP1 (input, output);
var number, guess : integer;
begin
number := 2;
writeln('Guess a number between 1 and 10');
readln( guess );
if number = guess then
begin
writeln('Lucky you. It was the correct answer.');
writeln('You are just too smart.')
end;
if number <> guess then writeln('Sorry, you guessed wrong.')
end.

program IF_GROUP2 (input, output);
var number, guess : integer;
begin
number := 2;
writeln('Guess a number between 1 and 10');
readln( guess );
if number = guess then
begin
writeln('Lucky you. It was the correct answer.');
writeln('You are just too smart.')
end
end.

(c) Shilpa Sayura Foundation 2006-2017