0% found this document useful (0 votes)
541 views

Loops Mrs

Uploaded by

api-297910907
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
541 views

Loops Mrs

Uploaded by

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

LOOPS

MRS. PALMER
There are three categories of
control structures.

 Sequential Statements
 Conditional Branching
statements
 Iteration/ Looping
TYPES OF LOOPS

There are various types of Loops, two out of three will be covered in
Grade 10 are;

For
While
Repeat until
FOR LOOPS – repeats instructions a
predetermined number of times . E.g
10-100 times.

WHILE LOOPS- repeats instructions as


long as some condition is true. This
type of loop is often programmed to
stop when the user enters some value ,
E.g. “quit” or “999”.
TYPES OF LOOPS

For (Bounded/Definite) Loop – used to repeat an action a limited


number of times. This loop is used when the number of times an
action should be repeated is known.
While (Unbounded/ Indefinite) Loop – used to repeat an action
multiple times. This loop is used when the number of times an action
should be repeated is unknown.
SYNTAX
FOR Variable = <beginning> TO <ending> DO
Action
<instructions to be repeated>
ENDFOR
HOW TO CONSTRUCT A FOR LOOP
Step 1
Read A,B,C Develop the algorithm normally,
Sum=A+B+C as if only one cycle is involved.
Average=Sum/3
Print “Average”, Average

Step 2
Place the For statement before
For the instructions to start and
Read A,B,C endfor to end the instructions.
Sum=A+B+C
Average=Sum/3
Print “Average”, Average
EndFor
HOW TO CONSTRUCT A FOR LOOP
Step 3 Determine what instructions are
to be repeated and use a new
For S=1 to 25 Do variable as the loop variable.
Read A,B,C
Sum=A+B+C
Average=Sum/3
Print “Average”, Average
EndFor
STEP CLAUSE
This indicates how much the loop variable is to be increased or
decreased by each time the loop is executed.
E.g. E.g with a step clause:
FOR num=1 TO 5 Do
Print num FOR i=10 TO 0 step -2 DO
END FOR Print i
END FOR
The computer would display: The computer would display:
1 10
2 8
3 6
4 4
5 2
0

The step clause of -2 decreases


the countdown by taking away 2
each time the loop is repeated.
UNKNOWN VALUES
Sometimes without telling you how many times to read a
problem the computer will say read N numbers or X
numbers.
E.g. Write an algorithm that reads N ages and for those that
are more than 65 say “Senior Citizen”.
How will we know the value of N?
IS there a way to get it?
We simply, get it from the User.

So let’s break down the steps given

Print “Please enter age”


Read age
If age >65 then
Print “Senior Citizen”
Endif
Read N
For i=1 to N Do
Read age
If age >65 then
Print “Senior Citizen”
Endif
Endfor
Write a structured algorithm to print the
message hello world 10 times.
EXAMPLE OF A FOR LOOP (IPO)
Input Process Output
For x = 1 to 10 do hello world
print “hello world” hello world
endfor hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
EXAMPLE OF A FOR LOOP (PSEUDOCODE)
Start
Declare
X as integer
for x = 1 to 10 do
print “hello world”
endfor
stop
EXAMPLE OF A FOR LOOP (FLOW CHART)
start

x=1

NO

For x = 1
to 10 do

YES
Hello
world

stop
ACTIVITY
Answer the following questions by writing the IPO chart and
Pseudocode;
1. Write a structured algorithm that prints the message “Are we
there yet” 100 times.
2. Write a structured algorithm that reads the marks obtained by 30
students in a test given that the pass mark is 50 and for each
mark prints whether the person passed or failed.
3. Write a structured algorithm that prints the even numbers between
2 and 20.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy