Jamaica Gleaner

Introducti­on of the Pascal language

- NATALEE A. JOHNSON Natalee Johnson teaches at Ardenne High School. Send questions and comments to kerry-ann.hepburn@gleanerjm.com

GOOD DAY, students. This is lesson 28 in our series of lessons. In this week’s lesson, we will continue to look at an introducti­on to the Pascal language.

In this lesson, we will be looking at how to code the following constructs using Dev Pascal: if statement, for - do and while -do loop structures. Before we begin, there are two key things I need to highlight when coding in Pascal.

1. 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 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:

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:

IF STATEMENT

You were already introduced to the If statement when we looked at conditiona­l statements used in pseudocode. Just to remind you, an If statement is used to test a particular condition(s) where if the condition is true a statement is executed, else if it is false, another statement is executed.

An example of an If statement in Pascal is shown below:

EXAMPLE 1

Write a program to read a number, find the square of the number and print the square of the number if is greater than 100 else output THE SQUARE IS TOO SMALL.

FOR LOOPS

Remember your ‘For’ loops are definite loops; whatever is placed in the blocked of the loop will be executed a specified number of times. Let’s look at an example of a ‘For’ loop using Pascal code.

EXAMPLE 2

Write a program to read 10 integer scores, find the sum of the 10 scores and output the sum.

PROGRAM SUM_OF_SCORES;

Uses wincrt; Var count, score, sum:integer; 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