Pascal Programming
Pascal Programming
Presented by:
Ms V. Brown
Programme Objectives
4. Program Layout
5. Pascal Programs
7. Arithmetic Operators
8. Control Structures
9. Conclusion
What is a programming language
Low Level
High Level
What is Pascal Programming
Language
Pascal High Level Language created in
1970 and is named after Blaise Pascal a
French Mathematician.
Pascal Comments:-
{ } everything with the curly brackets are
comments.
variable_name := value;
By default, variables in Pascal are not initialized
with zero. They may contain rubbish values.
Variables can be initialized (assigned an initial
value) in their declaration. The initialization is
followed by the var keyword and the syntax of
initialization is as follows −
var
variable_name : type = value;
Some examples are −
age: integer = 15;
taxrate: real = 0.5;
grade: char = 'A';
name: string = 'John Smith';
Declaring Constants
Looping
Control Sequence
Structures Control
Structures
Selection
Control
Structures
Program Layout
Statement
Program Layout
program <Program name>;
var <variable1>,<variable2>:<data type>;
begin
<code statement 1>;
<code statement 2>;
<code statement 3>;
<code statement 4>;
end.
First Pascal Program
program PascalFun;
begin
Writeln(‘Pascal Programming is Fun');
end.
Program Arithmetic;
Begin
writeln(5 + 7);
writeln(6 - 3);
writeln(5 * 2);
writeln(10 / 2:0:2);
writeln;
writeln(‘Press <Enter> To Quit’);
readln;
end.
Arithmetic Program2
program mulBy5;
if <condition> then
begin
do something;
end;
Single Selection – Cont’d
Lets modify the program for data input:
Program ifTest;
var num: integer;
Begin
write(‘Enter a number:’);
readln(num)
if num > 2 then
begin
writeln(num, ‘ Is Greater Than 2’);
end;
writeln(‘Will print every time’);
end.
If then Else – Double Selection
When decisions are made
sometimes one action must be
executed if the condition
evaluates to true and another
action executed if the condition
evaluates to false. This is known
as the if-then-else control
structure.
Double Selection
if <condition> then
begin
do something
end
else
begin
do something else;
end;
If then else – Program
Program ifTest;
var num: integer;
Begin
write(‘Enter a number: ‘);
readln(num)
if num > 10 then
begin
writeln(num, ‘ Is Greater Than 10’);
end
else
begin
writeln(num, ‘is less than 10’);
end;
writeln(‘Will print every time’);
end.
If then else If – Multiple
Selection
There are instances where based on a certain
value one of several alternatives can be
selected. This sort of circumstance can be
handled using:
Nested if – If-then-else-If
If then else If – Multiple Selection
If <condition> then
begin
do something;
end
else
If <condition> then
begin
do something else;
end
else
If <condition> then
begin
do something more;
end;
If then else If – Multiple Selection
Program Stadium;
Var stand : char;
rev : real;
specs: integer;
begin
writeln(‘Enter a stand’);
readln(stand);
writeln(‘Enter number of spectators’)
readln(specs);
If stand = ‘A’ then
begin
rev := specs * 100;
end
else
If then else If – Multiple
Selection
If stand = ‘B’ then
begin
rev := specs * 200;
end
else
If stand = ‘C’ then
begin
rev := specs * 300;
end
else
If then else If – Multiple
Selection
If stand = ‘D’ then
begin
rev := specs * 500;
end;
writeln(‘The stand is: ‘ stand);
Writeln(‘The revenue is: ‘ rev);
Iteration
In the instance when an action needs to be
repeated several times, the Pascal
Programming Language provides the iterative
structures known as loop.
Types of loops:
For
While
For Loops
A for loop is regarded as a pretest
counter controlled loop.
For loop:
check its condition before loop
statements are executed
repeats/countsfor a specified
number of times.
For Loops
program ForLoop;
var
i: Integer;
begin
for i := 1 to 10 do
begin
Writeln('Hello');
Writeln('This is iteration ',i);
end;
writeln(‘Outside of loop’);
end.
For Loops
Program ClassAverage;
{Write a program using Pascal to input the names and grades for a
class of 25 students. Calculate and print the class average}
Uses crt;
Var name : string;
grade, tgrade, cavg : real;
S : integer;
begin
cavg := 0;
For S := 1 to 3 Do
begin
Writeln('Please enter name of student');
Readln(name);
For Loops
Writeln('Please grade of student');
Readln(grade);
tgrade := tgrade + grade;
clrscr;
end;
cavg := tgrade/3;
Writeln(cavg:0:2,' is the Class Average');
Readln;
end.
While Loops
Thewhile loop repeats while a
condition is true.
begin
cavg := 0;
S := 1;
While S <=3 do
begin
Writeln('Please enter name of student');
Readln(name);
Writeln;
While Loops
Writeln('Please grade of student');
Readln(grade);
tgrade := tgrade + grade;
S:= S + 1;
clrscr;
end;
cavg := tgrade/3;
Writeln(cavg:0:2,' is the Class Average');
Readln;
end.