0% found this document useful (0 votes)
17 views

Programming With Pascal Grade 10

Uploaded by

faithngoma585
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Programming With Pascal Grade 10

Uploaded by

faithngoma585
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

KAPIRI GIRLS NATIONAL TECHNICAL SCH

Introduction to
computer programming
Programming with Pascal
Grade 10
Introduction to computer
programming

▪Computers are made up of hardware components


that in themselves are not capable of carrying out any
process of data to produce results.
▪Writing a computer program requires a programmer
to think logically and work accurately in order to
create the instruction for the hardware.
Computer Languages
▪A computer Language is set of instructions used
for writing computer programs.
▪Types of computer languages
1. High level
2. Assembly language
3. Machine language
High level language
▪ This is a programming language oriented towards the problem to
be solved or the procedures to be used. High level languages
contain statements that are written in English words and symbols.
▪ High level language source code needs a compiler or an
interpreter to convert the code into machine language that the
hardware can understand.
▪ Compiler converts the high level programming source code into
machine language .
▪ It is used to create an executable program from the source code.
▪ An interpreter is a program that is able to execute the program
instructions written in a high level language.
Low-level language
▪this is a machine oriented language which conforms to
the instructions and is restricted to be used on that
machine or this is a Programming language that is
more arcane and difficult to understand.
▪Some good examples of low-level languages are
assembly and machine languages.
Machine language
▪Computer work at the level of the 0s and 1s to store data
and retrieve instructions known as Machine Language
▪Sometimes referred to as machine code or object code,
machine language is a collection of binary digits or bits that
the computer reads and interprets. Machine language is
the only language a computer is capable of understanding.
▪this is the language that contain statements that are
written using mnemonic codes (codes suggesting their
meaning and easier to remember) to represent operations
and addresses that relate to the main memory and storage
registers of a computer.
Examples of Computer programming
Languages
▪Pascal
▪C++
▪Java
▪Python
▪Delphi
▪SQL
▪COBOL
▪Fortran
Exercise
1. What is a computer language?
2. State three types of programming languages.
3. Give two examples of programming languages.
4. Give one deference between Low-level and Machine
language.
5. What is a compiler?
MARIO MK CHONGO
Introduction to Pascal
▪In order to be able to write a computer program
in pascal you need a pascal compiler such as
Turbo Pascal 7.0 for windows 7
▪A compiler provides a space in which to type the
programming source code and it is used to
create the executable program file.
Pascal Program
Rules to write a program
▪Type 100% accurate
▪Pascal is not Case-sensitive
▪Pascal ignores blank lines.
Program structure

▪It is divided into two many section:


1. Program header
2. Instruction block
PROGRAM HELLO WORLD
(* program to print Hello world*) Program header
USES Crt;
(*variable declaration section*)
VAR .............; Variable declaration
…………..;
(*instruction block*)
BEGIN
Clrscr;
Instruction block
WriteIn(`Hello World`) ;
ReadIn;
END.
Exercise
1. State two sections of a Pascal program.
2. State two rules when writing a Pascal program.
3. Write the structure of a Pascal program
MARIO MK CHONGO
PROGRAM HEADER
❑PROGRAM HELLOWORD;
▪It is always the first line of a Pascal program.
▪It begins with the keyword PROGRAM followed by an
identifier (name) and a semicolon.
▪The identifier should describe the function of the
program.
▪The identifier starts with a letter.
COMMENTS
(* program to print hello world*)
▪ Comments appear in between (* and *) and are ignored by
the compiler during execution.
▪ They are also known as program documentation of the
program.
▪ They remind the programmer how they were thinking at a
particular point.
▪ They also serve as explanation to other programmers.
▪ They can also be used to exclude code that has a mistake.
VARIABLES DECLARATION
▪When the variable is declared, the compiler reserves
space for the variables in the primary memory.
▪Example
VAR
X, y : INTEGER;
Z : REAL;
INSTRUCTION BLOCK
▪It is the executable part of the program.
▪It starts with the reserved word BEGIN and finishes
with the reserved word END followed by a period (full
stop).
▪Example
BEGIN
…………
…………
END.
Exercise
1. Explain the use of program header.
2. As a programmer comments are very vital in a code.
Give three reasons why it is important to leave
comments in a code.
3. Define variable declaration.
4. Declare two variables x and y.
WriteIn procedure
▪ WriteIn (‘Hello word’);
▪ The reserved word WriteIn is the name of a standard procedure by
the means of which the computer can communicate with the user.
▪ What appears in the bracket () after the WriteIn command will
determine whether the display is reflected on the screen or on the
printer.
▪ Example
• WriteIn (‘Hello World’); will display Hello World on the screen
• WriteIn (lst, ‘Hello World’); will display on the printer because it has
lst after the open bracket.
Clrscr procedure
▪The reserved word Clrscr is a procedure that clears
the screen.
▪It make the output from the screen look neater.
▪In order to use the Clrscr command, the USES clause
must be included.
▪Example
USES Crt;
ReadIn Procedure
▪ It is a procedure that establishes communication between
the computer and the user.
▪ It causes the compiler to wait for input from the keyboard
before the execution of the program can be completed.
Consecutive Pascal statements
▪Consecutive Pascal statements within the
instruction blocks are separated by semicolons (;).
▪The statement can appear on the same line next to
each other separated by semicolons.
Indentation
▪Each statement within the instruction block is
indented to make it more readable.
Exercise

1. what is the function of WriteIn in the


program?
2. State the difference between WriteIn and
ReadIn in the program.
3. What is the use of reserved word Clrscr?
4. What is the use of ReadIn?
Display output on screen
Produce output on the screen using
the Write and WriteIn Statement
▪ The format of the WriteIn and Write statement,
Write (……………………);
WriteIn (…………);
WriteIn;
▪ The difference between Write and WriteIn statements
• After a Write statement has been executed, the output is produced from
the current position of the cursor but the cursor remains on the same line
of the output.
• After the WriteIn statement has been executed, the output is produced
from the current position of the cursor and the cursor moves to the next
line of output.
Code that will produce on one line
PROGRAM HELLO2;
(*program produces output on the same line*)
USES Crt;
BEGIN
Clrscr;
Write(`Hello World!`);
Write(`I am a new computer program.`);
ReadIn;
END.
OUTPUT
Hello World! I am a new computer program
Code that will produce second
line on its own line
WriteIn (`Hello World`);
write (`I am a new computer program`);
or
WriteIn (`Hello World`);
WriteIn (`I am a new computer program`);
Formatting output- aligning
output to columns
▪ The format output indicates how the information printed will be
presented. This includes spacing, the printing of blank lines and
column positioning.
WriteIn (`1234567890123456789012345`} {1}
WriteIn (`Surname`, `First Name` : 15); {2}
WriteIn (`Grade` : 20); {3}
OUTPUT
1234567890123456789012345
Surname First Name
Grade
How numbers behave in the
output
▪ Use Write and WriteIn statements to print numbers.
▪ When printing integers, real numbers or mathematical
expressions such as 3 + 6, 6.2 – 4 or 7 * 8. these are not
surrounded by single quotes. If they are surrounded by
single quotes then, they become strings constants instead of
numbers.
PROGRAM NUMBERS;
(*displaying integer and real numbers*)
USES Crt;
BEGIN
Clrscr;
WriteIn (`1234567890123456789012345`);
Write(34);
WriteIn (-50 : 10);
WriteIn (`The sum of 3 + 4 is : `, 3 + 4);
WriteIn (78 . 2 : 12 : 2);
WriteIn (45.3);
ReadIn;
END.
Display output on a printer
USES Crt, Printer;
BEGIN
Clrscr;
Write (lst, `Hello World!`);
WriteIn (lst, `I am a new program.`);
END.
PRE-DEFINED DATA TYPES AND VARIABLES IN PASCAL
The use of Variables
▪Variables are used as placeholders in the computer
memory in which certain values can be stored.
Data types
• Integer
• Boolean
• Character
• enumerated
Declare variable
▪ Variable declaration is a process of creating a variable,
stating what type of data it will store and giving it a name.
▪ Variables need to be given a unique identifier name.
▪ RULES FOR AN IDENTIFIER
• It must start with a letter
• It may contain letters, numbers or the underscore (_)
character.
• It may contain up to 127 character.
• It may not be a reserved word or a keyword.
Example of variable declaration
VAR no_days: INTEGER;
price: REAL;
name : STRING [10];
flag : BOOLEAN;
Initialise variables
▪Initialisation is a process of assigning a value to a
variable.
BEGIN
name : = `Mario`;
no_days : = 6;
flag : = true;
answer : = `Y`;
END.
▪The flag variable used as a signal in programming to
let you know that certain condition has been met.
▪The assignment operator in Pascal is :=.
▪e.g. Sum:= 60; means that the Sum variable has been
assigned to the value of 60.
BEGIN
no_days : = 6 * 24;
price : = 20 . 50;
sum : = sum+a
END.
Exercise
1. Define variable declaration.
2. Give two types of data types
3. Give an explanation of the data types
mentioned in question 2.
4. What is variable initialisation?
5. Initialise a variable x.
Using string variables
▪Variables that can store alphanumeric values that are
longer than one single character are known as strings.
▪Alphanumeric values can consist of the letters and
numbers.
▪The syntax for the string data type is string [number]
the number in brackets indicate the maximum length
of the string.
VAR name : STRING [10];
BEGIN
name : = `Dorica`;
(*will display the name Dorica*)
WriteIn(Dorica);
END.
Joining strings
VAR age : INTEGER;
BEGIN
age := 18;
(*will display I am 18 years old.*)
WriteIn(` I am `,age` years old.`);
END.
CONCAT function
▪You can join strings using the CONCAT function.
▪The general syntax is CONCAT (string1, string2..) or string 1
+ string 2+
▪The string is stored in an array structure, which allows you
to access each character of the string.
Name := `Dorica`;

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.

You might also like