Introduction to Pascal language
GOOD DAy, students. This is lesson 27 in our series of lessons. In this week’s lesson, you will be introduced to the Pascal language.
You would have learnt last week that the Pascal language is a high level language. Now you are going to learn how to write a program using the Pascal language and appreciate the first step of the implementation phase (translate the algorithm into a specific programming language). There are different versions of the Pascal program, such as Ezy Pascal, Dev Pascal, Turbo Pascal 1.5, Free Pascal, etc. You will work with the one selected by your teacher. The formats are similar for all versions with a few unique program syntax requirements. I will be using the Dev Pascal version for the upcoming lessons.
STRUCTURE OF A PASCAL PROGRAM
A Pascal program has three distinct parts:
1. The program heading
2. The program block
3. The program terminator (a period)
The program heading is a single statement beginning with the word ‘program’. The heading assigns a name to the program and lists the input and output streams in parentheses. The program block is the body of the program. The block is divided into two distinct parts:
1. The variable declaration section, where all the variables and data structures used by the program are defined (shown in program as var).
2. The statement section is where all the action statements of the program are specified. The statement section is encapsulated within ‘begin’ and ‘end’ statements.
Begin and end are examples of keywords used in Pascal. Keywords (or reserved words) are words that have special meaning in Pascal and can only be used in the predefined context. That is, they cannot be used as variable names or in any other context. Other key words are: program, type, var, const, read, write, readln, and writeln.
When you are adding comments to your program, they are included between parenthesis { } or between (* *) and can span multiple lines.
Below is a table comparing the keywords of a pseudocode versus the keywords used in Pascal.
KEY DISTINCTIONS IN PASCAL
The difference between READ and READLN.
There is no difference between ‘read’ and ‘readln’ when working with numeric values such as 2, 6, and 10. The difference applies when working with string values or characters on different lines such as ‘A’, ‘B’ and ‘C’. When the computer reads the first letter (‘A’) if you use the keyword READ, the program will not move to the next line to read B and C, so the keyword READLN is used to read in such values.
THE DIFFERENCE BETWEEN WRITE AND WRITELN
The keyword Write outputs text or values to the screen in a single line, leaving the cursor positioned at the end of line. On the contrary, Writeln, which means ‘write line’, outputs text or values to the screen in more than one line, leaving the cursor at the beginning of the next line instead of at the end of the current line. For example, if you were outputting the sentence ‘my first program’.
1. The first step in translating an algorithm into Pascal code is to make a list of all the variables used in the algorithm and determine their data type (i.e., the type of values that each variable is to store).
2. Translate the pseudocode into Pascal code based on the Pascal structure.
Let us now examine the translation of a pseudocode into the Pascal language using an example.
EXAMPLE 1 PSEUDOCODE VERSION
Algorithm: Item
This algorithm finds the total cost of an item.
Start
Declare quantity as data type integer
Declare unit_price and total_cost as real
Print “Enter the unit cost of an item followed by the quantity of the item” Read unit_price, quantity total_cost ? unit_price * quantity
Print “The total cost of your item is”, total_cost
Stop
PASCAL CODE
PLEASE NOTE: All program statements and lines are terminated with a semi-colon (;), except the ‘begin’ and ‘end’ keywords. Program statements preceding an end statement do not require a semi-colon (optional).
When outputting text to the screen, as oppose to pseudocode where double quotations are used, in Pascal single quotations are used. For example: Writeln(‘I Love to Program’);
PASCAL CODE WRITTEN USING DEV PASCAL
Writeln(‘Enter the unit cost of an item followed by the quantity of the item’); Readln(unit_price, quantity); total_cost := unit_price * quantity; Writeln(‘The total cost of your item is’, total_cost:0:2); We have come to the end of this lesson. Remember if you fail to prepare, you prepare to fail.