Pascal Programming

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 47

Pascal Programming

Presented by:
Ms V. Brown
Programme Objectives

1. Introducing Pascal Programming

2. Declaring variables & constants

3. Program Control Structures

4. Program Layout

5. Pascal Programs

6. Pascal Data Types


Programme Objectives Contd.

7. Arithmetic Operators

8. Control Structures

9. Conclusion
What is a programming language

A series of computer instructions written


according to a given set of rules or
conventions (“syntax”).

 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 was specially made for teaching


programming.

 Pascal was designed in 1968/9 and published in


1970 by Niklaus Wirth to encourage good
programming practices.
Putting Comments in your Programme
 Comments are used to explain what parts of a
program do. Comments are ignored by the
compiler and are only there for the people who use
the source code.

 Pascal Comments:-
{ } everything with the curly brackets are
comments.

// everything to the end of the line.


Naming Identifiers
 Variables and Constants are Identifiers that are
used to name memory location where a value
can be stored by a program.

 Identifiers cannot start with a digit.


Naming Identifiers
An Identifier is any valid combination of
characters. Identifiers can be any combination of
underscore (_), Digit (0 to 9), Upper or Lower case
letter (A to Z or a to z). (No other characters
allowed) The value of a variable can vary
(change).
Which are Valid
 Num1
 _#1
 Number 1
 1number
 _1
Reserved Words in Pascal

 The statements in Pascal are designed with some


specific Pascal words, which are called the
reserved words. For example, the words,
program, input, output, var, real, begin, readline,
writeline and end are all reserved words.
Declaring Variables

To declare a variable in Pascal the keyword


var is used followed by the variable name, a
colon (:) and then the data type of the
variable.

e.g. var num1:Integer;


Variable Initialization in Pascal

 Variables are assigned a value with a colon and


the equal sign, followed by a constant expression.
The general form of assigning a value is −

 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

To declare a constant in Pascal the keyword


const is used then the constant name, a colon
(:) and the data type of the constant followed
by the equal (=) and what is to be stored.

e.g. const gct:real = 17.5;


Program Control Structures

Looping
Control Sequence
Structures Control
Structures

Selection
Control
Structures
Program Layout

The program layout is divided into two


(2) sections:-
 Header
 Block
Program Layout

The block is further broken


down into two (2) parts:-
 Declaration

 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.

Type the above code, compile then execute.


 What happened?
 Did it work?
First Pascal Program - modified
program PascalFun;
 
begin
    Write(‘Pascal Programming is Fun');
Readln;
end.

What happened now?


Pascal Data Type

Name Type of Data Examples


String Holds Text 'New York',
Integer Holds whole numbers 3, 6, 1024
Real Holds Decimal Numbers 3.14, 503.2
Boolean Holds True or False TRUE,
FALSE
Character Holds a single character 'A', 'E'
Program 2
Arithmetic Operators
The Arithmetic Operators include:

    +          Addition


    -           Subtraction
    *           Multiplication
    /           Division
    Div       Integer Division
    Mod     Remainder
:= Assignment
Order of Calculations
Pascal follows the rules of the order of
operations.
1)Parentheses
2)Exponents
3)Multiplication
4)Division
5)Addition
6)Subtraction
Arithmetic Program1
Try the following:-

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;

var num, result:integer;


begin
writeln('Enter number to Multiply by 5');
readln(num);
result:=num * 5;
writeln(num,' * 5 = ',result);
writeln('Press <Enter> to quit');
readln;
end.
Decision Making
 Decisions are made using the if statement.

 Operators that can be used in conditions:


> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
= Equal to
<> Not equal to
If then – Single Selection
The syntax for the if statement is
as follows:

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.

A while loop does not need a loop


variable but if you want to use one
then you must initialize its value
before entering the loop.
While Loops
program Loops;
 
var
   i: Integer;
 
begin
   i := 0;
   while i <= 10 do
      begin
         i := i + 1;
         Writeln('Hello');
Writeln('This is iteration ',i);
      end;
writeln(‘Outside of loop’);
end.
While Loops
Program ClassAverage;
Uses crt;
Var name : string;
grade, tgrade, cavg : real;
S : integer;

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.

You might also like