0 ratings0% found this document useful (0 votes) 418 views9 pagesPascal Programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
Cire)
‘The programming language Pascal is used to teach
introductory computer programming in many
schools. This is because Pascal promotes a systematic,
well-organised and logical approach to learning
programming.
‘This chapter shows the practical aspeets of
implementing the programs introduced in
Chapter 10. If this is your first programming language,
you will need a few basics to get started. Think of a
programming language as a recipe for buking a cake
or instructions on building a bench. Pascal is one
programming language, just as English is one spoken
and written language.
Your mobile phone has « keypad that you use to type
the number or text message. Then you click send
and your call is made, or message sent, without the
knowledge of how it is done. However, if you type a
wrong digit when you are dialling a number, you will
not he able to contact the correct person.
Similarly, a computer uses an aplication called
a compiler to produee a result by translating the
instructions in a program you write into a form
the computer ean interpret: Ifyou type the wrong
instructions, che compiler eannot produce any results.
Instead it produees syntax errors or suntime errors.
Finding a Pascal compiler
‘There are a number of Pascal compilers available. In
this chapter, Ezy Pascal is used. You should search
online for ‘Ezy Pascal Free Download’ from Dolphin
Bay Software.
PROGRAMMING WITH PASCAL
Writing your first program
When you start Ezy Pascal, the Pascal editor will
appear (Fig 11.1). The upper green area is to show the
result of your programs, while the lower blue area is,
used for typing the program code.
Agni EeyPascalopennasceen
In your Pascal editor type the shore Pascal program
shown below: In the first fine, be sure to type your
name in place of ‘type your name here’ and today’s
date in place of type today’s date here’
{Name: type your name here}
{vitle: my first simple program written
in Pascal}
{Purpose: To output one Line}
{Date: type today’s date here}
Program Sentence;
Begin
Writeln(/Hello to everyone’);
End.‘The word Program tells the Pascal compiler that this
is the first line of eode that contains the name of the
program. In this example, the name of the program is
Sentence.
‘The main section of a program starts at the word
“Begin! and finishes at ‘End’. Within this section,
the specific instructions that need to be executed by:
the compiler are included to solve our programming
problem,
Mette ve creer
oe tat Yer Opens hn
Bea
,
ig 1.2 yong Pascal codeintothe editor
Notice the first four lines of code. These are comments
and are ignored by the Pascal compiler. You should
always sta:t your programs with the following
‘comments:
+ your name
the tide of the program
a brief deseription of the purpose of the program
the date the program was created.
Saving the program
‘You should save every program you type. To save the
program, click File, Save. Browse to where you want to
save your program (such as the Documents folder or
11.1 Introduction te Pascal
a memory stick). Then type the name ‘Sentence’ as the
name of the program. If the name of the program will
be two or more words, chen remove the spaces
between the words. So, for example, One Sentence!
will become ‘OneSentence’, Pascal will add the
defuult extension .pas to dhe progratn. You will see the
name of the program above the EzyPascal menu
(Fig 11.3).
BB Ezy Pascal Free Version Sentence.pas
£9 11.3 Your program is ghren aname ard shawmat the rapor the
xy arco sceen
Compiling the program
‘The Pascal compiler needs to convert the Pascal
program fiom the statements you typed in the editor
into machine language so that the central processing
unit can execute the program correctly.’The compiler
first checks your program to ensure that no typing
or syntax esrors were made, Select Run, Compile or
click the Compile icon on the menu. If you typed
everything correctly, the compiler will output a
message ‘Compile successful’ at the hottom of the
screen (Pig 11.4), Otherwise an error message will
be displayed (also at the bottom of the screen) and a
possible Iocation of the error will be highlighted ona
line of code.
Modified Compile successful al
Fai Comping ineprowarn shows amestzaect he bottom ofthese
tnatthere weno giaxerors
In Figure 11.5, the compiler has highlighted the line
immediately below the error. The orange arrow shows
where the error is located: the semicolon was omitted
at the end of the line.11 Prograrmming with Paseat
Running the program
Once you have no compilation errors, you can execute
(run) the program to see if it produces the correct
output. From the Run menu, select the Run option
or elick the Run icon, The program will execute in a
program as shown in Figure 11.6.
Fig 51 The compiler Pas highghted the re medal belo the ero
‘When compiling and removing errors, if you make
multiple corrections, and more errors appear, then you
may not know which correction caused the additional
errors. Therefore, it is best to make cortections in this
order:
1 Identify and correct each error individually.
2 Save the program.
3 Compile the progeam again. Figs Pour of theprogrom nae Serence
You should continue to compile the progeam until
there are no errors and the ‘Compilation successful?
message is shown.
Questions
1 State which of the following are valid names for a 2. List three steps to follow when compiling a
Pascal program: program.
a Add numbers
b 10Lines
© ForLoop
dd ThreeChoices
fe maxValues:Congratulations! Now that you have completed your
first Pascal prograts, you should compile and execute it
Table 11.1 Stricture of fascal program
amples)
Program header
Program name; Program Sample;
For€zxy Pascal
or
Program Sample input, output);
Forsome other compilers
Declerations
Const Const pi=3.14;
Var Mark Count: integer;
Test reat
Grade: char,
Title='Hello;
Procedures and functions
Begin Begin
Statements
Read(Mark);
Wiiteln( ThankYou}
1 (Miark> 30) Then Grade =P
Else Grade =F
Forcount = 1t0 5 Do.
Begin
Tests= Mari* count;
\Writeln(‘The test outputs; test;
End;
While Mark > 30.0
Begin
Wiiteln(This is @ sample while loop);
Wiiteln(More statements can ge here’)
End
Repeat
Wiiteln(this is a sample Repeat loop’
Witeln(More statements can go here!)
Until Mark > 36;
End,
End,
Structure of a Pa
eee
to sce your results, Table 11.1 shows the basic structure
of Pascal program,
‘All Pascal progrems must have a Program heading, The first
‘word, Program, is compulsory.
‘Const is short for constant: You assign a value to aconstant
when you create it,
Var is short for variable, Before you can use a variable you
musttfrst declare it. These exemples declare Mark and Count
as integer, Test as real and Grade as character. Variable types
include integer, real, character and literal (string),
Denotes the start of one or more programming statements
‘These include:
‘nour,
output,
‘conditional (F-THEN-ELSE)
assignment.
Forloop
Begin (ofa compound statement)
‘compound statements have two or mare consecutive
statements
Ends (of a compound statement)
While loon
Cen include compound statements enclosed in Begin and End
Repeat loop
‘Coninclude compound statements thet may not need Begin
and End
End. (with a full stop) indicates the very last line or end of the
program,11 Progamming with Pascal
Punctuation
‘The semicolon is used to separate declarations and nd sauce
most statements, Many Pascal programs are writwen co Else Writeln(‘this is within the
follow a structure, such as those in. previous examples. line’, Newline);
However,
declarations and statements, the following Pascal Ifa semicolon is placed
ce the semicolon separates the indivicnal
ith the End, this would cause
program will also compile and run: a syntax error for the II™THEN-ELSE statement.
Program Sentence; Begin Writeln( ‘Hello ‘The Last line of every Pascal program’s code must be
to everyone’); End. End.’ The full stop denotes the end of the program.
Comments in { } can however be written after this last
‘The keywords ‘Begin’ and ‘End’ act as brackets for
multiple statements, so there is no semicolon after begin
but usually after End wo show the endof thesttement. Statements in Pascal
There are some oceasions when no semicolon is used
statement.
Statements give insttuctions to the computer. A simple
statement is a single instruction while « compound
statement comprises two or more statements that
after End, a3 in the following example:
If (line >80) are within ‘Begin’ and End? keywords. A compound
Then Begin {perform the following two statement is useful if you need to perform some
statements}
instructions in sequence or repetitively before moving
on to the next set of statements. Various types of
statements are explained in Table 11.2.
Writeln( ‘This is over the line’);
Newline := Newline — line;
Table 11.2 Vypes of statements
Input statement Read: The cursor stays next to the text entered
Read and Readin reads datafrom keyboard, and Radin; ‘The cursor movesto the next line after the text is
‘waits forthe Enter key on the Keyboard to be pressed entered
Assignmentstatement Mark == 30; ‘= lscalled the assignment operator
Places data in a variable ‘The variable is on thellet ofthe assignment operator
The valueis on the right of the assignment operator:
The semicolon (3 ends the statement
Here, Mark is assigned the value 30
Output statement write; The cursor remains at the same place on the sereen
‘Shows the output on the monitor screen writeln ‘The cursor goes tothe start of the next line
Writer; Outputs 30
Wilteln (Mark); Outputs 30
| (jis the cursor)
Write(Score of\Merk]; Outputs Score ot 30 |
Wiiteln(Score of{Mark); Qutputs Score of 20
| (isthe cursor)
‘Compound statement Begin Begin (ofa compound statement)
Mark:5 Mark 75; Twoor more consecutive statements
Writeln(Mark); End; (of a compound statement)
End;