Jamaica Gleaner

The algorithmi­c language

- Natalee A. Johnson Urquhart CONTRIBUTO­R Natalee A. Johnson Urquhart teaches at Ardenne High School. Send comments to kerry-annhepburn@gleanerjm.com.

GOOD DAY, students. This is lesson 23 in our series. In this week’s lesson, we will continue to look at the representa­tion of algorithms.

PSEUDOCODE FORMAT INPUT STATEMENTS

The input statement is used to get data from outside the computer from a particular input device into a variable for manipulati­on by the pseudocode. The key words we will use for input will be ‘input’ or ‘read’. To read data into a variable, you write the word input or read, followed by the variable name. This is the way in which it will be written:

READ VARIABLE_NAME

The variable_name is the name given to the location where the value is to be stored. For example, if I am reading a number into a variable, my variable name would be ‘num’ and the read statement would, therefore, be:

‘Read num’

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: Print “The sum of the numbers is”, sum

Or

Print sum

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 psuedocode 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. Print “Please enter two numbers” Read num1, num2

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: sum + num1 + num2 Some assignment statements just involve assigning values to variables or initializi­ng, such as: Count + 1 Highest_Price + 0

CONTROL STRUCTURES USED IN STRUCTURED PROGRAMMIN­G

Structured programmin­g is the ability to express a problem solution using only three basic patterns of logic or control structures. These structures govern the order in which instructio­ns are carried out and specify the flow of control. They are:

Sequence

Selection

Iteration

Sequence: This is where one statement follows another in a step-by-step manner sequential­ly, and the program executes them in the sequence given. All programs are sequential by nature. See example below: Start

Read num1, num2

Sum + num1 + num2

Print sum Stop

Selection (conditiona­l branching): This is used where one of several alternativ­e programmin­g instructio­ns is selected and executed, depending on whether or not a particular condition is met.

EXAMPLE

If A > B then print A else print B

Endif

Iteration: This occurs when one or more programmin­g instructio­ns are performed repeatedly, depending on whether or not a particular condition is met.

TYPES OF ITERATION

(a) Bounded iteration (the loop is repeated a fixed number of times)

(b) Unbounded iteration (the loop is repeated an unknown number of times)

EXAMPLE

(1) For counter + 1 to 10 do read num sum + sum + num

Endfor (2) counter+ 2 while counter <= 50 do read num counter + counter + 2 endwhile (3) counter + 2 do

Read num counter + counter + 2 while counter <= 50 (4) counter + 1 Repeat read num counter + counter + 1 Until counter <= 20 We will look at each control structures in further detail in subsequent lessons.

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

Newspapers in English

Newspapers from Jamaica