Jamaica Gleaner

Control structures

FLOW CHART VERSION FOR EXAMPLE 1

- Natalee A. Johnson Urquhart CONTRIBUTO­R

GOOD DAY, students. This is lesson 24 in our series of lessons. In this week’s lesson, we will continue to look at the representa­tion of loop structures in pseudocode and flow chart, specifical­ly the ‘while’ loop.

Previous knowledge: You should be familiar with one type of loop structure represente­d in both pseudocode and flow chart format, and that is the for-loop structure, as well as the key elements that are usually included in a loop structure.

LOOP (ITERATION/REPETITION) THE ‘WHILE’ LOOP

The ‘while’ loop structure is shown below: While Variable not equal to Control_Variable/ValueDo

Block Statement(s)

Endwhile

The ‘while’ loop is an example of an indefinite loop (unbounded loop); it facilitate­s the repetition of a block of instructio­ns until a certain condition is met. No one knows exactly how many times the block statements (instructio­ns) will be carried out. Using the same example of having a bowl of ice cream with jello, no one can tell how many scoops you would take that will fill your stomach. It depends on the size of your stomach and the size of the spoon. The algorithm would look something like this:

FURTHER EXPLANATIO­N OF THE ‘WHILE’ LOOP STRUCTURE

The pretest is generally indicated first to be tested against the condition of the while loop. As long as the condition is false, whatever is located inside the loop structure will be executed. Now, in order for the loop to terminate or to allow the user to enter another value, and in the case of the example used above, for the user to continue eating the bowl of ice cream and jello, the post-test has to be present inside the loop. The post test is, thus, tested against the ‘while’ condition again (while stomach not full), and if the stomach is still not full, then the instructio­ns in the loop are repeated. On the contrary, if the person’s stomach is full, the loop will end.

EXAMPLE 2

Write a pseudocode algorithm to read a set of integer numbers terminated by 999. The pseudocode should find the sum and average of the numbers. The algorithm

should also output the sum and average of the numbers.

Algorithm Sum_and _Average

This program will read a set of integer numbers and calculate the sum and average of the numbers, and output the sum and average of the numbers.

The ‘while’ loop structure can have a pretest and post-test to help with the execution of the loop, which is terminated with a terminatin­g value; but in some instances, the ‘while’ loop will continue to execute until a certain value is reached (considered to be bounded), similar to the for-loop structure.

An example of a bounded ‘while’ loop

This ‘while’ loop will continue to execute as long as the count is less than or equal to 10. In this case, no pretest or post-test is necessary. The algorithm will terminate when the count exceeds 10.

Start count –– 1 num ––10 sum –– 20

While count<= 10do sum –– sum + num num –– num + 5 count –– count + 1 Endwhile

Print sum Stop

We have come to the end of this lesson. See you next week, when we will look at the use of operators and trace tables. Remember, if you fail to prepare, you prepare to fail.

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

Newspapers in English

Newspapers from Jamaica