Jamaica Gleaner

Introducti­on to Pascal language

- Natalee A. Johnson Urquhart CONTRIBUTO­R

GOOD DAY, students. This is lesson 29 in our series of lessons. In this week’s lesson, you will be introduced to the Pascal language.

You would have already learnt 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 implementa­tion phase (translate the algorithm into a specific programmin­g 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 requiremen­ts. 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 parenthese­s. The program block is the body of the program. The block is divided into two distinct parts:

1. The variable declaratio­n 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 encapsulat­ed 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 parenthesi­s { } or between (* *) and can span multiple lines.

Below is a table comparing the keywords of a pseudocode versus the keywords used in Pascal.

KEY DISTINCTIO­NS 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. As opposed to ‘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”. Using the ‘Write’ keyword it would look like this: Using the keyword Writeln it would appear on the screen like this:

TRANSLATIN­G PSEUDOCODE INTO PASCAL CODE

1. The first step in translatin­g 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. Please to note the following:

NAMING VARIABLES

Similar to what you learnt with pseudocode, a variable must begin with a letter and then be followed by any digit, letter or the underscore character.

No character space is allowed when naming your variables. See the diagrams below which illustrate good variable names versus bad variable names.

You cannot use the name of your program as a variable in the actual program. This will be treated as a duplicate identifier (variable name).

2. FORMATTING REAL VALUES

Pascal allows for real numbers to be formatted to a specified number of decimal places. Let’s say you were adding a set of real numbers and then you were required to find the average of the real numbers and print the average. The program line would look like this: Write(‘The Average is’,Average:4:2)

Let’s say num1 = 9 and num2 = 4 and as such, the average would be 2.25. Without specifying the character spacing and decimal places, the output would look like this: On the other hand, the statement: Write(‘The Average is’, Average:4:2) would look like this on the screen.

Let us now examine the translatio­n 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

We have come to the end of this lesson. Remember, if you fail to prepare, you prepare to fail.

 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??

Newspapers in English

Newspapers from Jamaica