Brackets in pseudocode expressions

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

Brackets in pseudocode expressions

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly calculates the average of two marks, Mark1 and Mark2?
A Average ← (Mark1 + Mark2) / 2
B Average ← Mark1 + Mark2 / 2
2. A is 6 and B is 10. What is the value of A + B / 2?
A 8
B 11
3. A is 6 and B is 10. What is the value of (A + B) / 2?
A 8
B 11
4. Which statement about brackets in an expression is correct?
A The work inside the brackets is done first
B The work inside the brackets is done last
5. Which condition checks that a person is 18 or over AND has a ticket or a pass?
A Age >= 18 AND Ticket = TRUE OR Pass = TRUE
B Age >= 18 AND (Ticket = TRUE OR Pass = TRUE)

Match the Pairs

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

Left columnRight column
1Average ← (A + B) / 2 AResult ← A + B / 2
2IF (Age > 12) AND (Age < 20) OR (Member = TRUE) THEN BAverage ← (Mark1 + Mark2 + Mark3) / 3
3Without brackets, division is done before addition, so only B is divided by 2 CWithout brackets, AND is worked out before OR, so the condition may not mean what was intended. Brackets are needed, such as (Age < 12 OR Age > 65) AND Member = TRUE.
4Brackets make an average of three marks correct by adding all the marks before dividing DBrackets group the parts of the condition so the order in which AND and OR are applied is clear
5Average ← Total1 + Total2 / 2 EThe brackets are missing, so only Total2 is divided by 2 and Total1 is added afterwards. It should be (Total1 + Total2) / 2.
6IF Age < 12 OR Age > 65 AND Member = TRUE THEN FAdds A and B first because of the brackets, then divides the total by 2 to give the average

Definitions

(A + B) / 2

Fill in the Blanks

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

1.
Average ← _Mark1 + Mark2) / 2
2.
Average ← (A + B + C) _ 3
3.
IF (Day = "Sat" OR Day = "Sun") ___ Free = TRUE
4.
DECLARE Average : REAL Average ← (Mark1 + Mark2) / _
5.
IF Age > 15 AND _Pass OR Paid) THEN

Spot the Error

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

Question 1
A Average ← Mark1 + Mark2 / 2
B OUTPUT Average
C OUTPUT Mark1, Mark2
Question 2
A INPUT Vip, Cash, Card
B // must pay by Cash or Card
C IF Vip AND Cash OR Card THEN
D OUTPUT "Entry"
E ENDIF
Question 3
A INPUT A, B, C
B Mean ← (A + B + C / 3)
C OUTPUT Mean
Question 4
A Price ← 20
B Disc ← 5
C Qty ← 2
D Cost ← Price - Disc * Qty
E OUTPUT Cost
Question 5
A INPUT X
B // X must be from 1 to 10
C Valid ← TRUE
D IF NOT X > 0 AND X < 11 THEN
E Valid ← FALSE
F ENDIF

Answer Key

Brackets in pseudocode expressions
Section A: Quick Questions
Q1
A — Average ← (Mark1 + Mark2) / 2
Q2
B — 11
Q3
A — 8
Q4
A — The work inside the brackets is done first
Q5
B — Age >= 18 AND (Ticket = TRUE OR Pass = TRUE)
Section B: Match the Pairs
Q1
Average ← (A + B) / 2 → Adds A and B first because of the brackets, then divides the total by 2 to give the average
Q2
IF (Age > 12) AND (Age < 20) OR (Member = TRUE) THEN → Brackets group the parts of the condition so the order in which AND and OR are applied is clear
Q3
Without brackets, division is done before addition, so only B is divided by 2 → Result ← A + B / 2
Q4
Brackets make an average of three marks correct by adding all the marks before dividing → Average ← (Mark1 + Mark2 + Mark3) / 3
Q5
Average ← Total1 + Total2 / 2 → The brackets are missing, so only Total2 is divided by 2 and Total1 is added afterwards. It should be (Total1 + Total2) / 2.
Q6
IF Age < 12 OR Age > 65 AND Member = TRUE THEN → Without brackets, AND is worked out before OR, so the condition may not mean what was intended. Brackets are needed, such as (Age < 12 OR Age > 65) AND Member = TRUE.
Section C: Definitions
Q1
(A + B) / 2 — Brackets force the work inside to be done first, so A and B are added before dividing by 2. Without them, A + B / 2 divides B by 2 first. Use them in sums and AND/OR conditions.
Section D: Fill in the Blanks
Q1
Average ← (Mark1 + Mark2) / 2
Q2
Average ← (A + B + C) / 3
Q3
IF (Day = "Sat" OR Day = "Sun") AND Free = TRUE
Q4
DECLARE Average : REAL Average ← (Mark1 + Mark2) / 2
Q5
IF Age > 15 AND (Pass OR Paid) THEN
Section E: Spot the Error
Q1
A — Without brackets, Mark2 / 2 is done first and then added to Mark1. The addition must be done first, so it needs brackets: (Mark1 + Mark2) / 2.
Q2
C — AND is done before OR, so this means (Vip AND Cash) OR Card. Anyone with Card would get in without being a Vip. The OR part needs brackets: Vip AND (Cash OR Card).
Q3
B — The bracket closes in the wrong place, so only C is divided by 3 and then added to A and B. The closing bracket should come before the division: (A + B + C) / 3.
Q4
D — Multiplication is done before subtraction, so only Disc is multiplied by Qty. The discount must be taken off first, so it needs brackets: (Price - Disc) * Qty.
Q5
D — Without brackets, NOT applies only to X > 0, not to the whole range check. The whole condition must be in brackets: NOT (X > 0 AND X < 11).