Jamaica Gleaner

Control structures

- Natalee A. Johnson Urquhart CONTRIBUTO­R

GOOD DAY, students. This is lesson 25 in our series. In this week’s lesson, we will begin to look at control structures/statements.

LOOP (ITERATION/REPETITION)

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

When we want the computer to repeat some statements several times, we need a loop structure to instruct the computer 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 (block) 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 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 namely three main types of loop constructs that we will focus on and they are: ‘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 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 the use of a counter. Counter –– 0

Counter –– Counter + 1

In the example above, the 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 which would be entered and then totalled, such that only 10 numbers will be entered. Please note 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 an example of having a bowl of jello with ice cream; you could have at least 20 scoops of the ice cream with jello. The algorithm would look something like this:

EXAMPLE 1

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

EXAMPLE 2 PSEUDOCODE VERSION

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

ALGORITHM SUM

This program will read 10 numbers and find the sum of those numbers.

Declare number, counter and Sum as data type integer

FLOWCHART VERSION

We have come to the end of this lesson. See you next week when we will continue to look at loops. Remember, if you fail to prepare, you prepare to fail. Natalee A. Johnson Urquhart teaches at Ardenne High School. Send comments to kerry-ann. hepburn@gleanerjm.com.

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

Newspapers in English

Newspapers from Jamaica