You are on page 1of 2

Pascal Programming

Pascal is a high-level, procedural programming language / Division


widely used as a language to learn general programming DIV Rounded Division
concepts. Pascal is a compiled language. It is not a case MOD Modulus / Remainder
sensitive language.
Comparison Operators
Pascal compiler: http://www.freepascal.org. Outputs a “True” or “False” value
Identifiers > Greater than
>= Greater than or equal
An identifier is a name given to a variable or a constant. The < Less than
following rules are followed when assigning identifiers. <= Less than or equal
= Equal
 Reserved words are not allowed <> Not equal
 Identifiers can only contain letters, numbers and the
underscore. Logical Operators
 Identifiers should start with an English letter and should
not contain spaces between words. AND Both conditions must be true.
OR At least one condition must be true.
NOT True becomes False and vice versa

Operator Precedence
Data Types in Pascal
When executing pascal expressions. The order of which the
Data types identify the type of content a variable, constant operators are executed is known as the operator
or and array can have inside the memory. precedence.

 Integer – positive or negative whole numbers. 1. NOT


 Real – Positive or negative decimal numbers. 2. * , / , DIV , MOD , AND
 Boolean – True or false values 3. + , - , OR
 Char – A single character 4. = , <> , < , <= , > , >=
 String – A sequence of characters. Writing Pascal Programs
Variables  The read() and readln() functions are used to input and
A variable is a memory location used by the program during assign values to variables.
execution. These are identified by name known as an  The write() and writeln() functions are used to display
‘identifier’. These values my change during the program text or results.
execution.  “:=” is the assignment operator.
 Each statement is terminated by a semicolon (;)
Variables are declared in the  { } are (* *) used for comments.
var section of the program.  Uses crt; allows to execute
o clrscr;
Constants
o readkey;
Constants do not change their values during the execution o gotoxy(x,y);
of the program. These are defined within the ‘const’ section. o textcolor(red);

Const pi=3.147;
Selections (Conditional statements)
Operators
IF Conditions
The following basic types of operators are used in Pascal.
If N1 > N2 then
Algebra Operators Large := N1
Else
+ Addition Large := N2;
- Subtraction
* Multiplication

PRASANNA SILVA ORDINARY LEVEL ICT 1


ictm[i] := marks;
Case Statements end;

Case Marks of Sub programs


0..34 : Grade := ‘W’;
35..49 : Grade := ‘S’; A subprogram is a program unit/module that performs a
50..64 : Grade := ‘C’; particular task. There are two types of subprograms
65..74 : Grade := ‘B’;
75..100 : Grade := ‘A’; 1. Functions − Returns a single value.
Else 2. Procedures − Do not return a value directly.
Writeln(‘Invaid Marks’);
End; These subprograms are defined before the main program
begins and is called from the main body of the program.
Repetitions Functions
There are three types of loops in Pascal.
program exFunction;
1. For – do var a, b, ret : integer;
For count := 1 to 10 do function max(num1, num2: integer):
writeln(count); integer;
var result: integer;
For count := 10 downto 1 do begin
writeln(count); if (num1 > num2) then
result := num1
2. While – do else
number := 1; result := num2;
while number <= 10 do max := result;
writeln(number); end;
number := number + 1; begin
a := 100;
3. Repeat-until b := 200;
count = 0; ret := max(a, b);
Repeat writeln( 'Max value is : ', ret );
writeln (‘Pascal’); end.
count := count + 1
Until count > 5 Procedures

Arrays Program proctest;


Uses crt;
An array is a data structure that allows to store multiple
Var a,b,c,min:integer;
items of the same data type using a single identifier name.
Procedure findmin(x,y,z:integer;
var m:integer);
begin
if x<y then
m:=x
else
m:=y;
if z<m then
Defining an array m:=z;
Var marks: array[0..9] of integer; end;
begin
Assigning a value to an array element write(‘Enter three numbers: ‘);
marks[3]:=35; readln(a,b,c);
findmin(a,b,c,min);
Using a loop to access an array.
writeln(‘Minimum number: ’,min);
A for loop is used to traverse an array since it can iterate a
readkey;
specific number of times. end.
var ictm : array[1..40] of integer;
i,marks : integer;
for i := 1 to 40 do
begin
writeln(‘Enter marks’);
read(marks);

PRASANNA SILVA ORDINARY LEVEL ICT 2

You might also like