The Free Pascal Compiler and Lazarus can be downloaded at no cost at www.freepascal.org. Free Pascal and Lazarus are a Turbo Pascal / Delphi clone that can make programs for Windows, Macintosh (Mac OS X), and Linux.
The If Statement Expression Signs include:
< Less Than
<= Less Than or Equal To
> Greater Than
>= Greater Than or Equal To
= Equal To
<> Not Equal To
The syntax for the if statement is as follows:
If <condition> then
begin
do something;
end;
This is a code snippet of an if statement in pascal:
if 5 > 2 then
begin
writeln(‘5 Is Greater Than 2’);
end;
The output will be:
5 Is Greater Than 2
If the program was modified to:
if 5 < 2 then
begin
writeln(‘5 Is Less Than 2’);
end;
There will not be any output because 5 is not less than 2.
If statements can have multiple conditions. This is referred to as if - else statements. This is the syntax for the if else statement.
If <condition> then
begin
do something;
end
else if <condition> then
begin
do something;
end
else
begin
do something;
end;
This is an example of a program that uses if else statements.
program FPTutorial5;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes
{ you can add units after this };
var
num1:integer;
num2:integer;
begin
write('Enter First Number: ');
readln(num1);
write('Enter Second Number: ');
readln(num2);
writeln;
if num1 > num2 then
begin
writeln(num1, ' Is Greater Than ', num2);
end { num1 > num2}
else if num1 < num2 then
begin
writeln(num1, ' Is Less Than ', num2);
end (* num1 < num2 *)
else
begin
writeln('The Numbers Are The Same');
end; // else
writeln;
writeln;
writeln('Press <Enter> To Quit');
readln();
end.
One thing to note. There are comments placed inside the last program. Comments are ignored by the computer but are used to write notes to the programmer(s). There are three comments supported by Free Pascal. They include
(* *) - anything within this is commented out by the compiler
{ } - anything within this is commented out by the compiler
// only the text after the double slashes and on the same line is commented
Video: Free Pascal Tutorial 5 - If Statements - Lazarus (9:44)