Jamaica Gleaner

Control structures

- Natalee A. Johnson Urquhart CONTRIBUTO­R

GOOD DAY, students. This is lesson 23 in our series of lessons. In this week’s lesson, we will begin to look at the representa­tion of loop structures in pseudocode and flow charts.

PREVIOUS KNOWLEDGE

The students should be familiar with data types and the two main ways of representi­ng an algorithm.

LOOP (ITERATION/REPETITION)

Most of the things we do in our everyday lives require some form of repetition, like getting ready for school or work. You perform the same steps over and over five to seven days a week.

When we want the computer to repeat some statements several times, we need a loop structure to instruct in on what to repeat and how often these steps are to be repeated.

Every loop has four major elements. These are:

■ Initialisa­tion

■ Repetitive statement(s)

■ Loop statements (blocks)

■ Conclusion

INITIALISA­TION

Before a loop is started, we may need some statements to get started. For example, we may need to initialise a variable to a start value or read an initial value into a variable.

REPETITIVE STATEMENTS

These are the statements that the computer program will repeat.

LOOP BLOCK

We must specify what statements are to be repeated by the computer. The repetitive statements are normally placed in the loop block.

There are three main types of loop constructs that we will focus on: for loop, while loop, and repeat until.

Key things to note when working with loops

THE USE OF AN ACCUMULATO­R

For example, you may be given a loop structure where you are required to find the sum of 10 numbers. You would agree that it would be time-consuming to sit and memorise the 10 numbers entered in order to add them.

With the use of an accumulato­r, you do not need to write down or try to memorise the numbers. As in the case of the ‘for loop’ (see explanatio­n of a ‘for loop’ below), you can start sum with the value 0, and each time you are given a new number, you can add it to your present sum, hence the statement sum –– sum + num. If the first number entered is 40, your sum would be 40 initially. Therefore, sum –– sum + num would be sum –– 0 + 40 = 40.

If you then add another number, say 10, to your present sum, your new sum would be 50.

Sum –– sum (previously) + new_number

The process will continue until all the numbers have been totalled. The only value you will keep in your memory is the current sum.

COUNTERS

This is the process of counting the number of times a value is entered or a statement is carried out. You can also allow your counter to begin at 0 and then increment (increase accordingl­y). Here is an example of an assignment statement with use of a counter.

Counter –– 0 Counter –– counter + 1

In the example above, counter is initially set at 0, followed by another statement increasing the counter by one, which means that every time the assignment statement is executed, the value of the counter variable is increased by 1. Thus, the assignment statement will provide a mechanism for counting. Using the same for loop example, a counter would count and keep track of the 10 numbers that would be entered and then totalled, such that only 10 numbers will be entered.

Please note that you could start your counter at 2, 5, etc., depending on the algorithm.

Let us now examine each of these loop constructs, starting with the ‘for loop’ structure.

THE FOR LOOP

The FOR loop is an example of a definite loop; it facilitate­s the repetition of a block of instructio­ns a definite number of times.

The for loop structure is shown below:

For Control_Variable –– to Do

Block Statement(s)

Endfor

Let us look at the example of having a bowl of Jell-O with ice cream. You could have at least 20 scoops of the ice cream with Jell-O. The algorithm would look something like this:

EXAMPLE 1

For COUNTER –– 1 TO 20 do fill spoon with ice cream and jello lift spoon to mouth swallow ice cream with jello Endfor

Please note that you use the FOR LOOP when you have a block of statements that will be carried out a set number of times; otherwise, you would use a different loop construct.

EXAMPLE 2 PSEUDOCODE VERSION

Write an algorithm to read 10 numbers and output the sum of the numbers.

ALGORITHM ADDITION

This program will read 10 numbers and find the sum of those numbers. Declare number, counter, and sum as data type integer.

Sum –– 0

Counter –– 0

For counter –– 1 to 10 do Read number

Sum –– Sum + number counter –– counter + 1 Endfor

Print “The sum is”, Sum

FLOWCHART VERSION KEY TERMS/VOCABULARY

■ Loop

■ Initialisa­tion

■ Repetitive statement(s)

■ Loop statements (blocks)

■ Accumulato­r

■ Counter

We have come to the end of lesson 23. See you next week, when we will continue to look at loops (while and repeat). Remember, if you fail to prepare, you prepare to fail.

 ??  ??

Newspapers in English

Newspapers from Jamaica