Weekly Test 1 Solved
Weekly Test 1 Solved
Explain, using examples, why you would choose to use each type of loop.
Example 1 -
REPEAT
input age
UNTIL age >= 0
Reason for choice - This code ensures that age is entered at least once, it will continue
prompting until a value is entered. It also ensures that the age is valid, as in, it is a positive
value.
Example 2 –
Counter ← 10
WHILE Counter > 0
Print “*”
Counter = Counter - 1
ENDWHILE
Reason for choice – The loop ensures that * is printed 10 times and only when the counter
remains positive and it should not run if the condition is initially false.
2. Read this section of code that inputs the ages of people entering an event. The input
sequence is ended by inputting a negative value for age. The code outputs the number of
people at the event over the age of 18.
01 Num18 = 0
02 INPUT Age
03 WHILE Age >= 0 DO
04 IF Age >= 18 THEN
05 Num18 = Num18 + Age
06 ENDIF
07 ENDWHILE
08 PRINT Num18 – Age
Locate these errors and suggest code correction to remove each error. [4]
Error 4 -
Correction -