Indentation in pseudocode

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

Indentation in pseudocode

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. How far are statements inside a FOR loop body indented in Cambridge pseudocode?
A Two spaces
B Four spaces
2. Which line is indented correctly inside an IF statement? IF Score > 50 THEN OUTPUT "Pass" ENDIF
A THEN at two spaces, OUTPUT at four spaces
B THEN at four spaces, OUTPUT at two spaces
3. Which ELSE indentation is correct?
A ELSE (two spaces)
B ELSE (four spaces)
4. A WHILE loop contains an IF statement. How should the IF be indented?
A Four spaces, because it is inside the loop
B No indent, because IF is a keyword
5. The body of a FOR loop is not indented at all. What is the effect?
A The loop will not repeat any more
B The code is harder to read because the structure is not clear

Match the Pairs

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

Left columnRight column
1FOR Count ← 1 TO 3 OUTPUT Count NEXT Count AThe loop body is not indented. OUTPUT Count should be moved in by four spaces.
2IF Score > 50 THEN OUTPUT "Pass" ENDIF BWHILE N < 5 DO IF N = 3 THEN OUTPUT N ENDIF N ← N + 1 ENDWHILE
3Nested statements keep indenting: an IF inside a loop is four spaces in, and its THEN part goes further in CTHEN is indented by four spaces. It should be indented by only two spaces.
4ELSE lines up with THEN at two spaces, and the statements under each are indented four spaces DThe statement inside the loop body is indented by four spaces
5FOR Count ← 1 TO 3 OUTPUT Count NEXT Count ETHEN is indented by only two spaces under IF, and the statement inside is indented by four
6IF Age >= 18 THEN OUTPUT "Adult" ENDIF FIF Age >= 18 THEN OUTPUT "Adult" ELSE OUTPUT "Child" ENDIF

Definitions

indentation

Fill in the Blanks

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

1.
FOR Count ← 1 TO 5 OUTPUT _____ NEXT Count
2.
____ OUTPUT "Fail" ENDIF
3.
WHILE Total < 100 DO Total ← Total + 10 ________
4.
FOR Row ← 1 TO 3 ___ Col ← 1 TO 3 OUTPUT Row * Col
5.
REPEAT _____ Guess UNTIL Guess = 7

Spot the Error

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

Question 1
A FOR Count ← 1 TO 3
B OUTPUT Count * 2
C ENDWHILE
Question 2
A WHILE Score < 10
B Score ← Score + 1
C OUTPUT Score
D ENDWHILE
Question 3
A IF Age >= 18
B THEN
C THEN OUTPUT "Adult"
D ENDIF
Question 4
A IF Age >= 18 THEN
B THEN
C OUTPUT "Adult"
D ENDIF
Question 5
A IF Score > 50
B ELSE
C OUTPUT "Pass"
D ENDIF

Answer Key

Indentation in pseudocode
Section A: Quick Questions
Q1
B — Four spaces
Q2
A — THEN at two spaces, OUTPUT at four spaces
Q3
A — ELSE (two spaces)
Q4
A — Four spaces, because it is inside the loop
Q5
B — The code is harder to read because the structure is not clear
Section B: Match the Pairs
Q1
FOR Count ← 1 TO 3 OUTPUT Count NEXT Count → The statement inside the loop body is indented by four spaces
Q2
IF Score > 50 THEN OUTPUT "Pass" ENDIF → THEN is indented by only two spaces under IF, and the statement inside is indented by four
Q3
Nested statements keep indenting: an IF inside a loop is four spaces in, and its THEN part goes further in → WHILE N < 5 DO IF N = 3 THEN OUTPUT N ENDIF N ← N + 1 ENDWHILE
Q4
ELSE lines up with THEN at two spaces, and the statements under each are indented four spaces → IF Age >= 18 THEN OUTPUT "Adult" ELSE OUTPUT "Child" ENDIF
Q5
FOR Count ← 1 TO 3 OUTPUT Count NEXT Count → The loop body is not indented. OUTPUT Count should be moved in by four spaces.
Q6
IF Age >= 18 THEN OUTPUT "Adult" ENDIF → THEN is indented by four spaces. It should be indented by only two spaces.
Section C: Definitions
Q1
indentation — Statements inside a loop or IF are moved in by four spaces, but THEN and ELSE are moved in by only two. Nested statements keep indenting, which makes the structure easy to read.
Section D: Fill in the Blanks
Q1
FOR Count ← 1 TO 5 OUTPUT Count NEXT Count
Q2
ELSE OUTPUT "Fail" ENDIF
Q3
WHILE Total < 100 DO Total ← Total + 10 ENDWHILE
Q4
FOR Row ← 1 TO 3 FOR Col ← 1 TO 3 OUTPUT Row * Col
Q5
REPEAT INPUT Guess UNTIL Guess = 7
Section E: Spot the Error
Q1
C — The closing keyword lines up with the FOR line at the left margin, and a FOR loop is closed with NEXT Count. ENDWHILE closes a WHILE loop. It should be 'NEXT Count'.
Q2
A — The WHILE header ends with DO, and the loop body is then indented by four spaces underneath it. The header should be 'WHILE Score < 10 DO'.
Q3
C — THEN sits on its own line, indented by two spaces. The statement inside the IF goes on its own line, indented by four spaces. It should be ' OUTPUT "Adult"'.
Q4
A — THEN must not be written on the IF line. It goes on its own line, indented by two spaces. The first line should be 'IF Age >= 18'.
Q5
B — The line after the IF condition is THEN, indented by two spaces. ELSE only comes after the THEN statements. It should be ' THEN'.