Indentation and comments in pseudocode

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

Indentation and comments in pseudocode

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which symbol starts a comment in Cambridge pseudocode?
A //
B #
2. How far are the statements inside a FOR loop indented?
A Not indented, lined up with FOR
B Four spaces in from the FOR line
3. Where should ELSE be placed in an IF statement?
A Lined up with its IF
B Indented four spaces under the IF
4. Which is a correct comment on its own line?
A / Ask the user for a mark
B // Ask the user for a mark
5. Which line has a correct comment after a statement?
A Count <- Count + 1 // add one to count
B // Count <- Count + 1 add one to count

Match the Pairs

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

Left columnRight column
1// Add the mark to the total AThe loop body is not indented. OUTPUT Count should be indented four spaces so it is clearly inside the loop.
2Total <- Total + Mark // running total BA comment on its own line. The computer ignores it; it explains the code to a human reader.
3The body of a FOR loop is indented four spaces so the repeated statements stand out CELSE is indented. It should line up with its IF at the left edge.
4ELSE lines up with its IF, and the statements in each branch are indented four spaces DFOR Count <- 1 TO 5 OUTPUT Count NEXT Count
5FOR Count <- 1 TO 3 OUTPUT Count NEXT Count EIF Mark >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF
6IF Age >= 16 THEN OUTPUT "Adult" ELSE OUTPUT "Child" ENDIF FA comment after a statement. The assignment still runs and everything after // is ignored.

Definitions

//

Fill in the Blanks

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

1.
__ Ask the user for a name INPUT Name
2.
FOR Count <- 1 TO 5 OUTPUT Count __ show the number NEXT Count
3.
IF Mark >= 50 THEN ____OUTPUT "Pass" ENDIF
4.
WHILE Num > 0 DO Num <- Num - 1 ________
5.
FOR Count <- 1 TO 3 __ Print the count OUTPUT Count

Spot the Error

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

Question 1
A total <- 0
B FOR i <- 1 TO 5
C INPUT n
D total <- total + n # add
E NEXT i
Question 2
A WHILE n > 0 DO
B /* count down by one */
C n <- n - 1
D ENDWHILE
Question 3
A IF age >= 16 THEN
B OUTPUT "Adult"
C ELSE THEN
D OUTPUT "Child"
E ENDIF
Question 4
A REM Add up five marks
B total <- 0
C FOR i <- 1 TO 5
D total <- total + i
E NEXT i
Question 5
A IF score > 50 THEN
B / score is a pass
C OUTPUT "Pass"
D ENDIF

Answer Key

Indentation and comments in pseudocode
Section A: Quick Questions
Q1
A — //
Q2
B — Four spaces in from the FOR line
Q3
A — Lined up with its IF
Q4
B — // Ask the user for a mark
Q5
A — Count <- Count + 1 // add one to count
Section B: Match the Pairs
Q1
// Add the mark to the total → A comment on its own line. The computer ignores it; it explains the code to a human reader.
Q2
Total <- Total + Mark // running total → A comment after a statement. The assignment still runs and everything after // is ignored.
Q3
The body of a FOR loop is indented four spaces so the repeated statements stand out → FOR Count <- 1 TO 5 OUTPUT Count NEXT Count
Q4
ELSE lines up with its IF, and the statements in each branch are indented four spaces → IF Mark >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF
Q5
FOR Count <- 1 TO 3 OUTPUT Count NEXT Count → The loop body is not indented. OUTPUT Count should be indented four spaces so it is clearly inside the loop.
Q6
IF Age >= 16 THEN OUTPUT "Adult" ELSE OUTPUT "Child" ENDIF → ELSE is indented. It should line up with its IF at the left edge.
Section C: Definitions
Q1
// — Starts a comment in Cambridge pseudocode. Statements inside a loop or IF are indented four spaces, and ELSE lines up with its IF, so the structure is easy to read.
Section D: Fill in the Blanks
Q1
// Ask the user for a name INPUT Name
Q2
FOR Count <- 1 TO 5 OUTPUT Count // show the number NEXT Count
Q3
IF Mark >= 50 THEN OUTPUT "Pass" ENDIF
Q4
WHILE Num > 0 DO Num <- Num - 1 ENDWHILE
Q5
FOR Count <- 1 TO 3 // Print the count OUTPUT Count
Section E: Spot the Error
Q1
D — A comment after a statement starts with //, not #. The line should be ' total <- total + n // add'.
Q2
B — Cambridge pseudocode comments start with //. The /* ... */ style belongs to other languages. The line should be ' // count down by one'.
Q3
C — ELSE stands on its own line, lined up with its IF. Only IF takes THEN. The line should be 'ELSE'.
Q4
A — Cambridge pseudocode starts a comment with //, not REM. The line should be '// Add up five marks'.
Q5
B — A comment needs two slashes, //. A single / would be read as code. The line should be ' // score is a pass'.