Programming With Pascal Grade 10
Programming With Pascal Grade 10
Introduction to
computer programming
Programming with Pascal
Grade 10
Introduction to computer
programming
Name [1] Name [2] Name [3] Name [4] Name [5] Name [6]
D o r i c a
Basic operators in Pascal
1. Arithmetic operators
2. Logical operators.
Arithmetic operator
▪ + addition
▪ - subtraction
▪ * multiplication
▪ DIV integer division
▪ / real division
▪ MOD modulus
VAR a, b : INTEGER;
c : REAL; OUTPUT
BEGIN
Clrscr; 12
a := 5; b := 3; c := 3.5; -3
WriteIn (a+7);
WriteIn (6-9);
10.50
WriteIn (b*c: 7 : 2); 2
WriteIn (a div b);
WriteIn (a/b : 7 : 2);
WriteIn (a mod b);
ReadIn;
END.
▪Division by zero is not permitted.
▪When you divide an integer by another integer
value, the answer is always rounded off and the
decimal is discarded.
Exercise
1. What is the output of the following x, y, and z?
VAR x, y, z : INTEGER;
BEGIN
x := 5;
y := 2;
z := 8;
WriteIn(z mod 2);
WriteIn(z DIV x);
WriteIn(x*y);
WriteIn(x+z);
ReadIn;
END.
Obtain basic user input
▪The ReadIn statement allows data values that exist
independently of the program to be entered via the
keyboard and associated with the variable declared in
the program.
▪Syntax
ReadIn;
ReadIn(name of the variable)
Example
VAR num1 : INTEGER;
BEGIN
Clrscr;
WriteIn (`Enter a number:`) (*massage to
enter a value*)
ReadIn (num1); (*read and store value in
num1*)
END.
Overwriting values in Variables
▪ Overwriting is the process of changing a value stored in a
variable and discarding the old value that it held before.
▪ Example
VAR num : INTEGER;
BEGIN
num: = 10;
(* 1 is added to the value in num and saved over the
*original value*)
Num : = num+1;
WriteIn (`Num: `,num); (* Displays 11 in this case*)
END.
Exercise
1. What is the output of the following code.
VAR x, y : INTEGER;
BEGIN
x := 10;
y := 4;
x := y;
WriteIn (x);
END.
More advanced programs and
concepts
MARIO MK CHONGO
Define constants
▪ Constants are fixed values that the program may not change. Such
Pi (Ꙥ) .
PRGRAM find;
USES Crt;
CONST
maximum = 20;
VAR x, answer : INTEGER;
BEGIN
answer : = maximum – x;
WriteIn (answer); (* Displays 15 in this case*)
ReadIn;
END.
User-defined data types
▪It is done because
• If the predefined data type is too long and you want
to shorten it.
• You use a particular data type too often and you want
to use an easy-to-remember reference to the data
type.
▪We use the keyword TYPE to define a new data type.
PROGRAM datatypes;
USES Crt;
TYPE hours =1 ..24;
caps= `A` ………`Z`;
VAR time ; hours;
letter : caps;
BEGIN
Clrscr;
time := 5;
WriteIn (`Time =`, time)
letter := `N`;
WriteIn(`Caps = `, letter);
WriteIn;
ReadIn;
END
Useful mathematical functions
▪Sqrt (x) determines the square root of number x.
▪Exp (power*In(base) to raise the number base to
the power of number power.
▪Example
5.23 will be determined as follows
Exp(3*In(5.2))
Example
VAR num1, num2 : INTEGER;
root, power: REAL;
BEGIN
num1 := 9;
num2 := 2;
root : sqrt (9);
power := exp (num2 *In (num1));
WriteIn (`The square root is :`, num1,`is `,root :2 : 0);
WriteIn ( ` The square is : `,power : 3 : 0);
ReadIn;
END.
REFERENCE
R Banda, B Dill, S Nunkumar (2015) Longman Computer Studies Pupil’s book 10: Longman Zambia Educational Publishers Ltd,
MARIO MK CHONGO
Lusaka Zambia.