Indenting statements by four spaces

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

Indenting statements by four spaces

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which FOR loop is laid out correctly in IGCSE pseudocode?
A FOR Count ← 1 TO 5 OUTPUT Count NEXT Count
B FOR Count ← 1 TO 5 OUTPUT Count NEXT Count
2. How far should a statement inside a WHILE loop be indented?
A 4 spaces
B No indent, level with WHILE
3. Which REPEAT loop is indented correctly?
A REPEAT INPUT Guess UNTIL Guess = 7
B REPEAT INPUT Guess UNTIL Guess = 7
4. A FOR loop sits inside another FOR loop. How is the inner loop's body indented?
A 8 spaces: 4 for the outer loop plus 4 for the inner loop
B 4 spaces, the same as the outer loop's body
5. Where should NEXT, ENDWHILE or UNTIL be placed?
A Level with the FOR, WHILE or REPEAT that it closes
B Indented four spaces like the loop body

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 5 OUTPUT Count NEXT Count AThe loop body is not indented. OUTPUT Count should start with 4 spaces to show it belongs to the FOR loop. This is a presentation error.
2FOR Row ← 1 TO 3 FOR Col ← 1 TO 3 OUTPUT Row * Col NEXT Col NEXT Row BThe OUTPUT line is indented by 4 spaces to show it is inside the FOR loop and repeats each time
3The body of a WHILE loop is indented by 4 spaces, and ENDWHILE lines up with WHILE CREPEAT INPUT Score OUTPUT "Score is ", Score UNTIL Score > 50
4Inside a loop, a statement that wraps onto a second line has its continuation line indented by 2 more spaces DNested loops: the inner FOR is 4 spaces in and its body is 8 spaces in, because each nested loop adds 4 more spaces
5FOR Count ← 1 TO 5 OUTPUT Count NEXT Count EWHILE Total < 100 DO Total ← Total + 10 ENDWHILE
6FOR Row ← 1 TO 3 FOR Col ← 1 TO 3 OUTPUT Row * Col NEXT Col NEXT Row FThe body of the inner loop is only indented by 4 spaces, level with the inner FOR. It needs 8 spaces because each nested loop adds 4 more.

Definitions

4 spaces

Fill in the Blanks

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

1.
FOR Count ← 1 TO 5 ____OUTPUT Count NEXT Count
2.
WHILE Total < 100 DO Total ← Total + 5 ________
3.
FOR Row ← 1 TO 3 FOR Col ← 1 TO 3 ________OUTPUT Row * Col
4.
OUTPUT "Your total score for the quiz is ", __TotalScore, " points"
5.
REPEAT INPUT Guess _____ Guess = 7

Spot the Error

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

Question 1
A Total ← 0
B FOR Count ← 1 TO 5
C INPUT Num
D Total = Total + Num
E NEXT Count
Question 2
A total ← 0
B FOR Count ← 1 TO 3
C Total ← Total + Count
D NEXT Count
E OUTPUT Total
Question 3
A while Lives > 0 DO
B INPUT Move
C Lives ← Lives - 1
D ENDWHILE
Question 4
A REPEAT
B INPUT Password
C Count ← Count + 1
D output "Try again"
E UNTIL Password = "abc"
Question 5
A FOR Row ← 1 TO 3
B FOR Col = 1 TO 4
C OUTPUT Row * Col
D NEXT Col
E NEXT Row

Answer Key

Indenting statements by four spaces
Section A: Quick Questions
Q1
B — FOR Count ← 1 TO 5 OUTPUT Count NEXT Count
Q2
A — 4 spaces
Q3
B — REPEAT INPUT Guess UNTIL Guess = 7
Q4
A — 8 spaces: 4 for the outer loop plus 4 for the inner loop
Q5
A — Level with the FOR, WHILE or REPEAT that it closes
Section B: Match the Pairs
Q1
FOR Count ← 1 TO 5 OUTPUT Count NEXT Count → The OUTPUT line is indented by 4 spaces to show it is inside the FOR loop and repeats each time
Q2
FOR Row ← 1 TO 3 FOR Col ← 1 TO 3 OUTPUT Row * Col NEXT Col NEXT Row → Nested loops: the inner FOR is 4 spaces in and its body is 8 spaces in, because each nested loop adds 4 more spaces
Q3
The body of a WHILE loop is indented by 4 spaces, and ENDWHILE lines up with WHILE → WHILE Total < 100 DO Total ← Total + 10 ENDWHILE
Q4
Inside a loop, a statement that wraps onto a second line has its continuation line indented by 2 more spaces → REPEAT INPUT Score OUTPUT "Score is ", Score UNTIL Score > 50
Q5
FOR Count ← 1 TO 5 OUTPUT Count NEXT Count → The loop body is not indented. OUTPUT Count should start with 4 spaces to show it belongs to the FOR loop. This is a presentation error.
Q6
FOR Row ← 1 TO 3 FOR Col ← 1 TO 3 OUTPUT Row * Col NEXT Col NEXT Row → The body of the inner loop is only indented by 4 spaces, level with the inner FOR. It needs 8 spaces because each nested loop adds 4 more.
Section C: Definitions
Q1
4 spaces — Indent every statement inside a FOR, WHILE or REPEAT loop by 4 spaces. Nested loops add 4 more; wrapped continuation lines use 2. An unindented loop body is a presentation error.
Section D: Fill in the Blanks
Q1
FOR Count ← 1 TO 5 OUTPUT Count NEXT Count
Q2
WHILE Total < 100 DO Total ← Total + 5 ENDWHILE
Q3
FOR Row ← 1 TO 3 FOR Col ← 1 TO 3 OUTPUT Row * Col
Q4
OUTPUT "Your total score for the quiz is ", TotalScore, " points"
Q5
REPEAT INPUT Guess UNTIL Guess = 7
Section E: Spot the Error
Q1
D — This statement is inside the FOR loop, so it must be indented by four spaces, level with INPUT Num. It is also at the left margin and uses = instead of ←. It should be ' Total ← Total + Num'.
Q2
A — Total ← 0 comes before the loop, so it is not inside it and must not be indented. The identifier should also be in Pascal case. It should be 'Total ← 0'.
Q3
A — The WHILE line starts the loop and is not inside another loop, so it stays at the left margin. Only the statements between WHILE and ENDWHILE get four spaces. The keyword should also be in capitals: 'WHILE Lives > 0 DO'.
Q4
D — Every statement between REPEAT and UNTIL is indented by four spaces, not two, and the keyword OUTPUT is written in capitals. It should be ' OUTPUT "Try again"'.
Q5
B — The inner FOR loop is inside the outer loop, so it must be indented by four spaces. It also uses = instead of ← for the loop start. It should be ' FOR Col ← 1 TO 4'.