Académique Documents
Professionnel Documents
Culture Documents
Computer Studies
Source [Computer Studies & Information Technology by Chris Leadbetter & Stewart Wain
Wright]
Loops ( Contd)
Many Problems involve a process that needs to
be repeated a specific number of times.
Therefore it is useful to have a technique that
performs these loops.
Source [Computer Studies & Information Technology by Chris Leadbetter & Stewart Wain
Wright]
Types of Loops
Source [Computer Studies & Information Technology by Chris Leadbetter & Stewart Wain Wright ]
FOR…… NEXT Loop
This is used when the loop is to be repeated a
know fixed number of times.
A FOR loop keeps on carrying out a command or
commands, FOR a given number of times.
The counter is automatically increased each time
the loop is performed.
The commands to be repeated are sandwiched
between the FOR and NEXT and than END FOR
commands
Source [Computer Studies & Information Technology by Chris Leadbetter & Stewart Wain Wright ]
Example FOR…… NEXT Loop
Problem : Add 10 numbers together
Algorithm :
For COUNT = 1 to 10
Input NUMBER ( inner loop)
TOTAL = TOTAL + NUMBER
Next COUNT
Source [Computer Studies & Information Technology by Chris Leadbetter & Stewart Wain Wright ]
Example FOR…… NEXT Loop (Explanation)
ALGORITHM:
FOR NUMBER = 1 to 10
Print 13 x NUMBER
NEXT NUMBER
For Loop Diagram
For (A;B;D;C) A
FALSE
B
TRUE
C
WHILE…… DO Loop
The WHILE….DO Loop are used in preference to
the For….Next Loop.
It can also be used when we don’t know how
many times the loop is to be performed.
The loop is ended when a certain condition is
true.
This condition is checked before starting the
loop
Source [Computer Studies & Information Technology by Chris Leadbetter & Stewart Wain Wright ]
Example WHILE…… DO Loop
Source [Computer Studies & Information Technology by Chris Leadbetter & Stewart Wain Wright ]
Example WHILE… DO Loop
(Explanation)
It is possible for a WHILE…DO loop never to be
performed, If COUNT is given the value 10
before WHILE… DO Loop is begun.
Source [Computer Studies & Information Technology by Chris Leadbetter & Stewart Wain Wright ]
REPEAT…… UNTIL Loop
The REPEAT … UNTIL Loop is used in
preference to the FOR…. NEXT Loop.
It can also be used when we don’t know
how many times the loop is to be
performed.
The Loop is ended when certain condition
is true
Source [Computer Studies & Information Technology by Chris Leadbetter & Stewart Wain Wright ]
REPEAT…… UNTIL Loop
The “TRUE” Condition is checked at the
end of the loop .
Source [Computer Studies & Information Technology by Chris Leadbetter & Stewart Wain Wright ]
Example REPEAT…… UNTIL
Loop
Problem : Add 10 numbers together
Algorithm :
Repeat
Input NUMBER
TOTAL = TOTAL + NUMBER
COUNT = COUNT + 1
Until COUNT = 10
Output TOTAL
Source [Computer Studies & Information Technology by Chris Leadbetter & Stewart Wain Wright ]
Selection or Conditional Statements
IF- THEN
This is often used when there is a simple
test available.
E.g. To test if a number is positive or
negative
Read number
IF number > 0 THEN write (‘Positive’)
Else write ( ‘negative’)
Selection or Conditional Statements
IF- THEN
Ans : 20
(b) Using Pseudocode or otherwise , write
an algorithm that will input the ID, weight
(kg) and height (m) of 30 students,
calculate their body mass index (BMI) and
output their ID, BMI and a comment as
follows:
Ans 40/10 = 4
b) The fuel economy for 1000 cars is to be
calculated using the formula in Question 16(A)
Write an algorithm, using pseudocode or
otherwise, which inputs the Distance travelled
(km) and the fuel used (litres) for 1000 cars.
The fuel economy for each car is then calculated
and the following outputs produced:
- Fuel Economy for each car
- Average (mean) Fuel Economy for all the
cars input
- The best fuel economy ( i.e. lowest value)
Total = 0, count =0, best =0 , worst =1000
Repeat
Input litres, distance
Economy = distance/litres
Print economy
If economy > best then best =
economy
If economy < worst then worst =
economy
Total = total + economy
Count = count +1
Until count = 1000
Average = total/1000
Print average, best, worst
Q The manufacturing cost of producing an item
depends on the complexity . A company
manufactures three different types of item, with
costs based on the following calculations.
Item type 1: item cost = part cost * 1.5
Item type 2: item cost = part cost * 2.5
Item type 3: item cost = part cost * 5.0
The company makes 1000 items per day
Write an algorithm, using pseudocode, flowchart
or otherwise , which
- Inputs the item type and parts cost of each
item
- Outputs the item cost for each item
- Calculates and outputs the average (mean)
item cost per day (based on 1000 items
being made).
Total cost = 0
For x = 1 to 1000
Input type , partcost
If type = 1 then itemcost = partcost * 1.5 }
If type = 2 then itemcost = partcost * 2.5 }
If type = 3 then itemcost = partcost * 5.0 }
else print error
totalcost = totalcost+ itemcost
print itemcost
next x
average = totalcost / 1000
print average