0% found this document useful (0 votes)
68 views3 pages

Loop Statements Handout

The For loop repeats a block of instructions a definite number of times. It has four major elements - the initialization, condition, increment/decrement, and body. An example is given of using a For loop to eat cornflakes 10 times, initializing a counter at 1, checking if the counter is less than or equal to 10 each repetition, incrementing the counter by 1 each time, and having the body contain the actions of filling a spoon, putting cornflakes in the mouth, chewing, and swallowing. The While loop is used when the number of repetitions is unknown.

Uploaded by

Ockouri Barnes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views3 pages

Loop Statements Handout

The For loop repeats a block of instructions a definite number of times. It has four major elements - the initialization, condition, increment/decrement, and body. An example is given of using a For loop to eat cornflakes 10 times, initializing a counter at 1, checking if the counter is less than or equal to 10 each repetition, incrementing the counter by 1 each time, and having the body contain the actions of filling a spoon, putting cornflakes in the mouth, chewing, and swallowing. The While loop is used when the number of repetitions is unknown.

Uploaded by

Ockouri Barnes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Statements

LESSON 4
TOPIC: WRITING PSEUDOCODE ALGORITHMS– LOOP
STATEMENTS

The For Loop


 It is a definite loop. Loop
Illustr using
ation the
of Cornf
The lakes
Whil Exa
e mple
 It repeats a block of instructions a definite
number of times.
While Stomach not full do
Layout of The For Loop
Fill spoon with cornflakes i
B

c
l

d
o

Put cornflakes in mouth


For CONTROLVARIABLE = 1 to N do e
c

f
k

i
n

Chewcornflakes e

d
f
t
t

i
e

o
n

endfor
u

endwhile
Swallow
n
o

b
c

u t
s e

e
i
u a
t r
t
o
s

r n

Illustration of The For Loop using o


n

s
f

the Cornflakes Example NOTE:


Use the While loop when you donot know
For Counter = 1 to 10 do
exactly howmany times the blockof statements
will be carried out. In this case there is somec

Fill spoon with cornflakes a


B

r
l

r o

terminating conditions. c

Put cornflakes in mouth i

e
k

Chew cornflakes t
f

m
t
n

s
e

Swallow e

Four Major Elements of Loop


x

endfor
u

sr t

Statements
l
NOTE:
y
i
 Initialization Before a loop is started we
mayneedsome
o

n
t

e
s
n

Use the For loop when you have a block of


statements to get started. For example, we
statements that will be carried out a set
number of times.  Repetitive Statements We need a statement that
may need to initialize a variabletoastartvalue. will instruct the
The While Loop
computer to repeat instructions. Inthe
 It is an indefinite loop
cornflakes examples, these statements
 It repeats a block of instructions until a
were the for – endfor andthe
certain condition is met
while – endwhile statements.
Layout of the While Loop
 Loop Block
We must specify what statementsaretobe
While CONDITION do
Statement(s) endwhile  Conclusion When a loop is over we
repeated. mayneedtoperformsome tasks. For example,
wemayneed to print the results of calculations.
However, not all loops needconclusionstatements.
22 FPowell [email protected]
COUNTING
THINGS TOCONSIDERWHEN
Counting involves increasing the value by a
fixed amount repeatedly. MMINGQUESTIONTHATCONTAINSALOOP
CONSTRUCTINGASOLUTIONTOAPROGRA
The counter variable must be given an initial
value before the counter instruction is carried
1. What type of loop do I need?Definiteor
out so as to facilitate the calculation. The
indefinite?
initial value for the counter variable is usually
0. 2. Does the question require anyinput? Yesor no? If
the answer is yes thenfor aforloop your first input
Layout statement must take
counter variable = counter variable + 1
place somewhere immediatelyfollowing
the beginning of the loop. If your loopisa
Practice question
while loop, then you will havetousean
Read a number. Count the number. Print the
count. input statement before the beginningoftheloop and
towards the endof theloop.
Solution
3. Do I need to count anything?If yes, how
NOTE:
Count = 0
many? For each itemtobe countedyou
Variable names
Read Num
need to initialize each counter variable
for counters
should be chosen
Count = Count + 1
before the start of the loopandyouneedto
to reflect what is
Print Count put each counter construct insidetheloop.
being counted.

4. Do I need to sumor accumulateanyvalue?


If yes, howmany? For eachvaluetobe
TOTALLING
accumulated you need toinitializean
The cumulative total is a progressive total that
accumulator to 0 outside theloopandyou
is arrived at by adding a value to the current
need to place an accumulator construct
total to obtain a new total.
inside the loop.
Example of how this works
5. Is there any condition or conditions
Add 5, 6, 6, 9 and 1. specified by the question? If yes, eachcounter,
accumulator or print statement
0 + 5 = 5 (5 is added to 0 to obtain 5)
within the loop block, askyourself under
5 + 6 = 11 (6 is added to 5 to obtain 11)
what condition does it dothis?
11 + 4 = 15 (4 is added to 11 to obtain 15)
15 + 9 = 24 (9 is added to 15 to obtain 24)
6. Do I need to print anything? If yes, where
24 + 1 = 25 (1 is added to 24 to obtain 25
do I put my print statement? Insidethe
loop or outside the loop? Aprint
The cumulative total is stored in a variable.
statement placed inside the loopwill be
The variable is assigned a value of 0 and as
executed each time the loopblockis
each value is added, a new cumulative total
results. Layout 7. What action must I
performwhentheloopterminates? This depends
repeated. onwhat isasked
cumulative variable = 0
by the question.
cumulative variable = cumulative variable + variable

23 FPowell [email protected]

You might also like