Jamaica Gleaner

Pseudocode algorithms

- 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 20 of our series of lessons. In this week’s lesson, we will continue to look at algorithms.

In the previous lesson, we started to examine the parts of an algorithm in more detail, and we started with input statements. Let us continue with that discussion, starting with output statements.

OUTPUT STATEMENTS

The output statement is used to get informatio­n to the programmer or to the user. The key words we will use for output are: ‘display’, ‘print’ and ‘output’. To output informatio­n to the user, you write the word Print, a statement followed by a variable_name or the word Print followed by a variable_name.

This is the way in which it will be written:

Print ‘statement’, variable_name or

Print variable_name

For example, if you were required to read two numbers, find the sum of the numbers and output the sum of the numbers, the output statement would be:

OUTPUT STATEMENTS

We also have a statement called the prompt statement. A prompt statement is actually an output statement which displays on the screen, to the user, a message indicating what actions to take based on the program written. For example, you may be asked to write a pseudocode algorithm to accept two numbers and prompt the user to enter the numbers (this would be done via the keyboard). This is how you would write the prompt statement.

ASSIGNMENT STATEMENT

Assignment statements are used to give initial value to variables and to change the value assigned to a variable. The assignment statement has two parts, the Left value and the Right value. The Left value refers to the variable as the storage location where the Right value will be stored. The Right value refers to a value which may be the result of an expression or the content of another variable. The assignment statement will be written as follows:

variable_name = expression

An assignment statement may involve an arithmetic operation such as: While some assignment statements just involve assigning values to variables (initializa­tion), such as:

Count ? 1 Highest_Price ––– 0

CONTROL STRUCTURES (SEQUENCE, SELECTION, ITERATION/REPETITION)

These statements are used to control the amount of time a statement or sequence of statements is carried out based on some condition. Sequencing involves writing a set of instructio­ns in the intended order you wish to solve a problem. This is generally done in a step-by-step manner, starting from the top of the algorithm to its ending point. This concept is applied to all algorithm solution. We will look at selection and iteration in subsequent lessons.

An algorithm can be represente­d using a pseudocode, narrative or a flow chart. Below is an example of a pseudocode representa­tion of an algorithm, where you will be able to observe the different parts of an algorithm we have discussed thus far.

EXAMPLE 1

Write a pseudocode algorithm to find the square of a number. Output the square of the number.

Let us first revisit the defining diagram used to analyse the problem and, subsequent­ly, the translatio­n of the solution in a pseudocode format.

We have come to the end of this lesson. See you next week, when we will continue to look at pseudocode algorithms. Remember, if you fail to prepare, you prepare to fail.

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

Newspapers in English

Newspapers from Jamaica