A Pascal program typically consists of the following parts:
program ExampleProgram;
uses crt;
var
a, b, sum: integer;
begin
a := 10;
b := 20;
sum := a + b;
writeln('Sum is: ', sum);
end.
Variables are declared in the var
section and must have a specified data type.
var
x: integer;
y: real;
z: char;
s: string;
program VariableExample;
var
age: integer;
price: real;
initial: char;
name: string;
isStudent: boolean;
begin
age := 25;
price := 19.99;
initial := 'A';
name := 'Alice';
isStudent := true;
writeln('Age: ', age);
writeln('Price: ', price:0:2);
writeln('Initial: ', initial);
writeln('Name: ', name);
writeln('Is Student: ', isStudent);
end.
Constants are declared in the const
section and must have a fixed value.
const
PI = 3.14159;
MAX_SIZE = 100;
Pascal supports various operators for arithmetic, relational, and logical operations.
+
, -
, *
, /
, div
, mod
=
, <>
, <
, <=
, >
, >=
and
, or
, not
program OperatorExample;
var
a, b, c: integer;
x, y: boolean;
begin
a := 10;
b := 20;
{ Arithmetic Operations }
c := a + b;
writeln('a + b = ', c);
c := b - a;
writeln('b - a = ', c);
c := a * b;
writeln('a * b = ', c);
c := b div a;
writeln('b div a = ', c);
c := b mod a;
writeln('b mod a = ', c);
{ Relational Operations }
if a < b then
writeln('a is less than b');
if a <> b then
writeln('a is not equal to b');
{ Logical Operations }
x := true;
y := false;
writeln('x and y = ', x and y);
writeln('x or y = ', x or y);
writeln('not x = ', not x);
end.