Repetition:
1.4 Logical structure “Repetition”
A part of the program is repeated many times and the result of a test condition will determine when to stop the repetition.
There are 3 types of repetition :-
- Fixed repetition (FOR … TO … DO)
The iteration occurs a fixed number of times - Non-zero-based conditional (REPEAT … UNTIL)
The iteration continues until a condition is satisfied. Since the condition test is at the end of the iteration, the iterated statements are executed at least once. - Zero-based conditional (WHILE … DO)
The iteration continues while a condition is true. If the condition is false to begin with, the iteration will occur zero times, i.e. not at all
WHILE … DO | REPEAT … UNTIL | |
1. | The condition is evaluated before each execution of the iterated statements. | The condition is evaluated after each execution of the iterated statements. |
2. | The iterated statements may not be executed at all. | The iterated statements are executed at least once. |
3. | The iteration is terminated when the condition is false. | The iteration is terminated when the condition is true. |
1.4.1 Zero-based conditional Iteration (while …. do…)
Example WDRep1
Problem :- Design an algorithm to process any number of orders as in example Seq2. End of data will be indicated by a product number of 999999
Solution :-
Data Required :- (i) product code
(ii) quantity ordered
(iii) unit price
Processing involved :- cost = quantity ordered * unit price
Output Required :- total cost
Algorithm :- (only pseudocode shown; as a supplementary exercise do the flowchart)
Read product code
While product code <> ‘999999’ Do
Read quantity ordered
Read unit price
cost = quantity ordered * unit price
Print the cost
Read another product code
Endwhile
Dry run
Perform a dry run on the algorithm and fill in the trace table below. Use the following product numbers, quantities and prices as test data.
000001,15,100,000008,10,5,000235,42,8,000045,12,75,000482,10,25,
142583,20,15,047256,315,10,012540,15,200,502100,210,120,999999
Product number | Is product number = ‘999999’? | Quantity | Unit price | Cost |
000001 | no | 15 | 100 | 1500 |
000008 | no | 10 | 5 | 50 |
000235 | ||||
Pascal Program
program wdrep1_process_orders;
var
qty : integer;
price, cost : real;
product : string[6];
begin
write(‘Please key in the product number > ‘);
readln(product);
while product <> ‘999999’ do
begin
write(‘Please key in a price > ‘);
readln(price);
write(‘Please key in quantity required > ‘);
readln(qty);
cost := qty * price;
writeln;
writeln (‘Product Quantity Price Cost’);
writeln (product:5, qty:10, price:11:2, cost:13:2);
write(‘Please key in product number > ‘);
readln(product)
end
(* endwhile *)
end.
Example WDRep2
Problem :- To modify the example NSeq1 so that the results of any number of student can be calculated. In addition, the students’ index number must be input. End of data will be indicated by an index number of ‘00000’
Solution :-
Algorithm :- (only pseudocode shown; as a supplementary exercise do the flowchart)
Read student’s index number
While index number <> ‘00000’ Do
Read marks for Project
Read marks for Theory exam
If mark for project = 0 Then
output result as “absent”
Else
total marks = project + theory
output the total marks
If total marks < 40 Then
output result as “failed”
Else
output result as “passed”
Endif
Endif
Read another index number
Endwhile
Dry run
Perform a dry run on the algorithm and fill in the trace table below. Use the following index numbers, project marks and theory marks as test data.
25436,23,68,12365,0,65,75698,15,58,12354,12,71,12542,13,23,
24535,21,19,10240,15,24,10245,0,35,96521,20,50,00000
Index number | Is index number = ‘00000’? | Project marks | Theory marks | Total marks | Results |
25436 | no | 23 | 68 | 91 | Passed |
12365 | no | 0 | 65 | ||
Pascal Program
program wdrep2_project;
var
index_no :integer;
project,theory,sum :real;
begin
write(‘Please enter student index number > ‘);
readln(index_no);
while index_no <> ‘00000’ do
begin
read(project);
if project = 0 then
writeln(‘Absent’)
else
begin
write(‘Please enter marks for Theory exam ‘);
readln (theory);
sum:=project + theory;
writeln (‘The sum is ‘,sum);
if sum < 40 then
writeln(‘Failed’)
else
writeln(‘Passed’)
end
write(‘Please enter another index number > ‘);
readln(index_no)
end
(* endwhile *)
end.
1.4.2 Non Zero-based conditional Iteration (Repeat… Until…)
Example RURep1
Problem :- Modify example WDRep1 by using Repeat … Until…. Instead of While … Do… program construct.
Solution :-
Algorithm :- (only pseudocode shown; as a supplementary exercise do the flowchart)
Repeat
Read product code
Read quantity ordered
Read unit price
cost = quantity ordered * unit price
Print the cost
Until product code = ‘999999’
Pascal Program
program rurep1_order_for;
var
times, qty : integer;
price, cost : real;
product : string[6];
begin
repeat
write(‘Please key in product number > ‘);
readln(product);
write(‘Please key in a price > ‘);
readln(price);
write(‘Please key in quantity required > ‘);
readln(qty);
cost := qty * price;
writeln;
writeln (‘Product Quantity Price Cost’);
writeln (product:5, qty:10, price:11:2, cost:13:2)
until product = ‘999999’
end.
Dry run
Perform a dry run on the algorithm and fill in the trace table below. Use the following product numbers, quantities and prices as test data.
000001,15,100,000008,10,5,000235,42,8,000045,12,75,000482,10,25,
142583,20,15,047256,315,10,012540,15,200,502100,210,120,999999
Product number | Quantity | Unit price | Is product number = ‘999999’? | Cost |
000001 | 15 | 100 | no | 1500 |
000008 | 10 | 5 | no | 50 |
000235 | ||||
What is the problem with this algorithm?
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..