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

Loops

The document discusses different types of loops used in programming including for, while, and repeat loops. It provides examples of how to write pseudocode using each type of loop and explanations of when each loop is appropriate.

Uploaded by

9hnxxj24rg
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Loops

The document discusses different types of loops used in programming including for, while, and repeat loops. It provides examples of how to write pseudocode using each type of loop and explanations of when each loop is appropriate.

Uploaded by

9hnxxj24rg
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

PROBLEM SOLVING

REPETITION/LOOPS/ITERATION
LOOPS
A loop occurs when a statement or statements are repeated until a condition is met.

There are two types of loops:-


• Definite or Bounded Loop – these are loops where you know in advance how many times to
repeat the loop ( a fixed number of times) e.g. FOR loop

• Indefinite or Unbounded Loop – these are loops where you do not know in advance how
many times to repeat the loop e.g. WHILE or REPEAT loops
DEFINITE OR BOUNDED LOOP – FOR LOOP
The FOR loop is used only when the start value and the
end value are known. The general form of the FOR loop N.B The variable usually
is:- begins with 1 and changes
by 1 (increments by 1). The

FOR <variable> = <start value> TO <end value> DO loop terminates when the
variable reaches the end
start
value. ENDFOR denotes the
{statements to be repeated}
end of the loop and not the
ENDFOR
program.
WORKED EXAMPLE
Write pseudocode to output the message “ I must try my best in everything I do” 100
times.

Algorithm Focus
Uses Variable
i: integer
Start
FOR i= 1 to 100 DO
Display “I must try my best in
everything I do”
STOP
PRACTICE
Write pseudocode to print the message “I must pay attention in class” 500 times.

Algorithm Focus
Uses Variable
i: integer
Start
FOR I = 1 to 500 DO
Display “I must pay attention in class”
STOP
WORKED EXAMPLE
Write pseudocode to enter the bill of the first 100 customers. Calculate a 15%
discount for these customers and print the amount of their new bill.

Algorithm Appreciation
Uses Variables
i:integer
bill, discount, final: real
Start
FOR I = 1 to 100 DO
start
Display “ Please enter your bill
amount”
Get bill
discount = 15% * bill
final = bill – discount
Display final
EndFor
MORE PRACTICE
Write pseudocode to enter three marks of 25 students. Calculate and print the total
marks of each student.
Algorithm Results
Uses Variables
I, mark1, mark2, mark3, sum: integer

Start
FOR I = 1 to 25 DO
Start
Display “ Enter three marks”
Get mark1, mark2, mark3
sum = mark1 + mark2 + mark3
Display “ your total mark is”, sum
ENDFOR
STOP
MORE PRACTICE
Write pseudocode to enter the IT marks of 36 students. If the mark is greater than 50, output ‘pass’
otherwise output ‘fail’.
Algorithm Results
Uses Variables:
I , mark :integer

Start
For I = 1 to 36 DO
Start
Display “ Please enter your IT mark”
Get mark
IF mark > 50 THEN
Display “ pass”
ELSE
Display “fail”
EndFor
Stop.
INDEFINITE OR UNBOUNDED LOOPS
1. The WHILE loop repeatedly executes one or more statements as long as the condition
is true. The condition in a WHILE loop is tested at the beginning of the loop, so it is
possible for the statement not to be executed at all.

The general form of the WHILE loop is:-

WHILE <condition> DO
{statements to be repeated}
EndWhile.
WORKED EXAMPLE
Write pseudocode to output the message “ I must try my best in everything I do” 100
times.
Algorithm Focus N.B when a variable is
Uses variables set to zero it is called
i:integer
initialisation.
START
i=0 When 1 is added to
WHILE i < 100 DO
a variable it is called
Start
Display “I must try my best in everything I incrementing.
do”
i=i+1
EndWhile
STOP.
PRACTICE
Write pseudocode to display the message “ I must pay attention in class “ 200 times.

Algorithm Discipline
Uses Variables
i:integer
Start
i= 0
While I < 200 DO
Start
Display ““ I must pay attention in
class “
i= I + 1
EndWhile
WORKED EXAMPLE
Write pseudocode to enter the bill of customers until 999 is entered. Calculate a 15% discount for
these customers and print the discount and amount of their new bill.
Algorithm Store
Uses Variables
i: integer
bill, discount,NewBill:real;
Start Note: 999 is referred to as a
i= 0 dummy value.
WHILE I <> 999 DO
Start
Display “ Enter the amount of your bill”
Get bill
discount = bill * 15%
NewBill = bill – discount
Display “ You received a discount of “, discount
Display “ Your new bill is”, NewBill
Get i
endWhile
Stop
PRACTICE
Write pseudocode to enter the price of a car . Calculate motor vehicle tax at 25% of price until -1 is
entered. Print the motor vehicle tax and the price of the car when the motor vehicle tax is added.

Algorithm Government
Uses Variables
i: integer
price, tax, final:real
Start
i=0
WHILE I <> -1 DO
Start
Display “ Enter the price of the car”
Get price
tax = price * 25%
Display “ The motor vehicle tax is “, tax
final = price + tax
Display “ The final price of the car is “, final
Get I
EndWhile
STOP.
2. REPEAT LOOP
The REPEAT construct repeatedly executes one or more statements as long as a condition is
met. This condition is tested at the end of the loop so the statement will always be executed at
least once. The general format of the REPEAT statement is:-

REPEAT

{statements to be repeated}

UNTIL {condition}
WORKED EXAMPLE
Write pseudocode to output the message “ I must try my best in everything I do” 100 times.

Algorithm Focus
Uses variables
i:integer
START
i=0
REPEAT
Display “I must try my best in
everything I do”
i=i+1
Until I = 100
STOP.
PRACTICE
Write pseudocode to display the message “ I must pay attention in class “ 200 times.

Algorithm Discipline
Uses Variables
i:integer
Start
i= 0
REPEAT
Display ““ I must pay attention in
class “
i= I + 1
UNTIL i=200
STOP.
MORE PRACTICE
Write pseudocode to enter the price of a car . Calculate motor vehicle tax at 25% of price until -1 is
entered. Print the motor vehicle tax and the price of the car when the motor vehicle tax is added.
Algorithm Government
Uses Variables
i: integer
price, tax, final:real
Start
i=0
REPEAT
Display “ Enter the price of the car”
Get price
tax = price * 25%
Display “ The motor vehicle tax is “, tax
final = price + tax
Display “ The final price of the car is “, final
Get I
UNTIL I = -1
STOP.
THE END.

You might also like