Jamaica Gleaner

Control structures

- 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 24 in our series of lessons. In this week’s lesson, we will conclude looking at control structures and will also look at trace tables.

THE ‘REPEAT-UNTIL LOOP’

The repeat-until loop is similar to the while loop, except the condition is tested at the end of the loop (post-test). Thus, the block of statement(s) will continue to execute as long as the specified condition in the UNTIL statement is true. Using the same example of having a bowl of ice cream and jello, you would continue to have your ice cream and jello as long as you have ice cream in your bowl.

The repeat-until loop structure is shown below:

TRACE TABLE

A trace table is an important tool for testing the logics of a pseudocode for accuracy. A trace table is a rectangle array of rows and columns. The column headings are the variables in the pseudocode. As instructio­ns in the pseudocode are carried out and the variables are modified, the changes are recorded in the appropriat­e column in the table. When the pseudocode terminates, the final values in the trace tables should reflect the correct result.

Let us look at an example of how a trace table is executed using Example 1 shown below.

EXAMPLE 1

Write a pseudocode algorithm to read the ages of 10 persons and print the average age of the persons as well as the age of the oldest person.

1. For the example above, you will first initialize your variables as shown in the algorithm.

2. Then you would repeat step 2 to step 5 until the user enters the 10th value. The program will then stop.

3. The average will be calculated (80/10); average is 8.

4. Both the average and the oldest value will be printed, which is 8 and 20, respective­ly.

We have come to the end of lesson 24 in our series of lessons. See you next week, when we will look at relational operators and begin a new unit. Remember, if you fail to prepare, you prepare to fail.

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

Newspapers in English

Newspapers from Jamaica