Question 1 Report
A programmer writes a program that uses loops and selection.
(a) Describe the difference between a FOR loop and a WHILE loop. [2]
(b) State one situation where a FOR loop is more appropriate than a WHILE loop. Give a reason. [2]
(c) Describe what is meant by selection (IF-THEN-ELSE) in programming. [2]
(d) Describe what is meant by nested selection. Give an example scenario where it would be used. [2]
(e) Describe the difference between a WHILE loop and a REPEAT-UNTIL loop.
[2]
(f) Write pseudocode using a REPEAT-UNTIL loop that asks the user to enter a password until the correct password "abc123" is entered.
[2]
(g) Write pseudocode using nested IF statements for a grading system that assigns grades based on a student's mark: 70 or above = "Distinction", 50-69 = "Merit", 40-49 = "Pass", below 40 = "Fail".
[4]
(a) A FOR loop repeats a block of code a predetermined number of times. It uses a counter variable that is automatically initialised, incremented and checked by the loop structure itself. [1] A WHILE loop repeats a block of code as long as a specified condition remains true. The number of repetitions is not known in advance; the loop continues until the condition becomes false. [1]
(b) A FOR loop is more appropriate when the number of iterations is known in advance. For example, printing exam results for exactly 30 students is best handled by a FOR loop. [1] The loop counter automatically counts from 1 to 30, making the code simpler and avoiding the need for a separate counter variable and manual condition check. [1]
(c) Selection (IF-THEN-ELSE) allows the program to choose between different paths of execution based on whether a condition evaluates to true or false. [1] For example: IF a student's mark is 50 or above THEN display "Pass" ELSE display "Fail". The condition is evaluated, and only one of the two branches executes. [1]
(d) Nested selection is when one IF statement is placed inside another, allowing the program to test multiple conditions in a structured sequence. [1] For example, a ticketing system might first check if the customer is under 16 (child), and if so, check whether they are under 5 (free ticket) or aged 5-15 (child-rate ticket). Each subsequent condition refines the decision further. [1]
(e) A WHILE loop checks its condition before the loop body executes. If the condition is initially false, the body never executes at all. [1] A REPEAT-UNTIL loop executes the body first and then checks the condition afterwards. This guarantees the body always executes at least once, regardless of the condition. [1]
(f)
REPEAT
INPUT Password
UNTIL Password = "abc123"The REPEAT-UNTIL structure is ideal here because the user must be asked for input at least once before the password can be checked. [1] The loop continues prompting until the correct password is entered. [1]
(g)
INPUT Mark
IF Mark >= 70 THEN
OUTPUT "Distinction"
ELSE
IF Mark >= 50 THEN
OUTPUT "Merit"
ELSE
IF Mark >= 40 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
ENDIF
ENDIFThe nested IF structure tests conditions from highest to lowest. Once a condition is met, the corresponding grade is output and no further conditions are tested. [1] [1] [1] [1]
Everything you need to excel in your exams