Jamaica Gleaner

Introducti­on to the Pascal language

- NATALEE A. JOHNSON Contributo­r Natalee A. 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 be then 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.

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:

‘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 greater than 100, else output ‘the squares is too small’.

‘FOR’ LOOPS

Remember, your ‘for’ loops are definite loops; whatever is placed in the block 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.

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