Pseudocode: user interaction loop with a menu

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Pseudocode: user interaction loop with a menu

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which loop is the best fit for a menu that must show at least once and keep going until the user picks Exit?
A repeat ... until choice = 4
B for i from 1 to 4
2. In a menu loop, where should the line that reads the user's choice go?
A Before the loop only, once at the start
B Inside the loop, after the menu is printed
3. Which pseudocode reads the choice correctly, with the menu shown on each pass?
A repeat print menu input choice
B input choice repeat print menu
4. A menu has options 1, 2 and 3, and 4 is Exit. What should the program do if the user types 7?
A Print an invalid choice message and show the menu again
B Stop the loop because the choice isn't 1, 2 or 3
5. Which part of the menu structure handles the invalid choice message?
A The until condition at the end of the loop
B The final else after the else if options

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1repeat ... until choice = 4 Ainput choice
2else print "Invalid choice" BTells the user their entry is not a menu option, then the loop shows the menu again
3Each pass of the loop reads the user's menu choice so the program can select an action CThe menu is printed after the choice is read, so the user is asked for input before seeing the options. print the menu first, then input choice.
4A second menu option is checked with else if after the first option's if DThe menu offers 3 as the exit option but the loop ends on 4, so choosing Exit never stops the loop. The until condition must be choice = 3.
5repeat print "1 Add 2 View 3 Exit" input choice ... until choice = 4 Eelse if choice = 2 then
6repeat input choice print "Choose 1, 2 or 3" if choice = 1 then ... FKeeps showing the menu and acting on the choice until the user picks 4, the exit option

Definitions

repeat ... until choice = 4

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
______ print "1 Play 2 Scores 3 Help 4 Exit" input choice
2.
print "Enter your choice:" _____ choice if choice = 1 then
3.
if choice = 1 then print "Starting game" else __ choice = 2 then
4.
print "Help" ____ print "Invalid choice"
5.
print "4 = Exit" input choice until choice = _

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A repeat
B print choice
C if choice = 1 then
D print "Playing"
E end if
F until choice = 2
Question 2
A repeat
B print "1 Add 3 Exit"
C input choice
D until choice = 2
Question 3
A repeat
B print "1 Play 2 Quit"
C input choice
D until choice <> 2
Question 4
A repeat
B input choice
C if choice > 4 then
D print "Valid choice"
E end if
F until choice = 4
Question 5
A repeat
B input choice
C if choice = 2
D print "Viewing"
E end if
F until choice = 3

Answer Key

Pseudocode: user interaction loop with a menu
Section A: Quick Questions
Q1
A — repeat ... until choice = 4
Q2
B — Inside the loop, after the menu is printed
Q3
A — repeat print menu input choice
Q4
A — Print an invalid choice message and show the menu again
Q5
B — The final else after the else if options
Section B: Match the Pairs
Q1
repeat ... until choice = 4 → Keeps showing the menu and acting on the choice until the user picks 4, the exit option
Q2
else print "Invalid choice" → Tells the user their entry is not a menu option, then the loop shows the menu again
Q3
Each pass of the loop reads the user's menu choice so the program can select an action → input choice
Q4
A second menu option is checked with else if after the first option's if → else if choice = 2 then
Q5
repeat print "1 Add 2 View 3 Exit" input choice ... until choice = 4 → The menu offers 3 as the exit option but the loop ends on 4, so choosing Exit never stops the loop. The until condition must be choice = 3.
Q6
repeat input choice print "Choose 1, 2 or 3" if choice = 1 then ... → The menu is printed after the choice is read, so the user is asked for input before seeing the options. print the menu first, then input choice.
Section C: Definitions
Q1
repeat ... until choice = 4 — A menu loop: repeat shows the menu, reads the choice, picks an action with if or else if, and stops on the exit choice. Any other value gets an invalid choice message.
Section D: Fill in the Blanks
Q1
repeat print "1 Play 2 Scores 3 Help 4 Exit" input choice
Q2
print "Enter your choice:" input choice if choice = 1 then
Q3
if choice = 1 then print "Starting game" else if choice = 2 then
Q4
print "Help" else print "Invalid choice"
Q5
print "4 = Exit" input choice until choice = 4
Section E: Spot the Error
Q1
B — The menu loop must read the user's choice with input choice. print choice only displays a value that was never entered, so the if and the until have nothing to test. Line 2 should be input choice.
Q2
B — The menu says Exit is option 3, but the loop ends on choice = 2. The menu text must match the exit choice. Line 2 should print "1 Add 2 Exit".
Q3
D — A menu loop must stop when the user picks the exit choice. until choice <> 2 stops on any other choice and keeps looping on Quit. It should be until choice = 2.
Q4
D — This branch runs when the choice is not a menu option, so it must tell the user the choice is invalid. It should print "Invalid choice".
Q5
C — An if statement in pseudocode needs the keyword then at the end of the condition. Line 3 should be if choice = 2 then.