Displaying every element with OUTPUT in a loop

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

Displaying every element with OUTPUT in a loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which loop body displays every element of the StudentNames array in order?
A OUTPUT StudentNames[Index]
B OUTPUT StudentNames
2. StudentNames is declared ARRAY[1:30]. Which loop header visits every element?
A FOR Index ← 0 TO 30
B FOR Index ← 1 TO 30
3. True or false: OUTPUT StudentNames[Index] inside a FOR loop outputs a different element on each pass.
A False
B True
4. Which line shows the index and the mark together, like Mark 3: 72?
A OUTPUT "Mark ", Index, ": ", Marks[Index]
B OUTPUT "Mark Index: Marks[Index]"
5. Which loop header outputs the elements of Marks[1:10] in reverse order?
A FOR Index ← 10 TO 1 STEP -1
B FOR Index ← 10 TO 1

Match the Pairs

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

Left columnRight column
1OUTPUT StudentNames[Index] ADisplays a label, the index number and the element, all on one line, separated by commas.
2OUTPUT "Mark ", Index, ": ", Marks[Index] BDisplays the element at the current index. Inside a FOR loop it outputs every element in order.
3To display every element of an array in reverse order, the loop counts down using STEP -1. CFOR Index ← 1 TO 5 OUTPUT Names[Index] NEXT Index
4To display every element of a 5-element array in order, loop from the lower bound to the upper bound and output Names[Index]. DThe array name is used without an index, so it does not output one element per pass. It should be StudentNames[Index].
5FOR Index ← 1 TO 5 OUTPUT StudentNames NEXT Index EA fixed index of 5 is used instead of the loop variable, so the same element is output every time. It should be Marks[Index].
6FOR Index ← 1 TO 5 OUTPUT Marks[5] NEXT Index FFOR Index ← 10 TO 1 STEP -1 OUTPUT Marks[Index] NEXT Index

Definitions

OUTPUT StudentNames[Index]

Fill in the Blanks

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

1.
FOR Index ← 1 TO 5 OUTPUT ____________[Index] NEXT Index
2.
FOR Index ← 1 TO 5 OUTPUT StudentNames[_____] NEXT Index
3.
FOR Index ← 1 TO 10 OUTPUT "Mark ", Index, ": ", _____[Index] NEXT Index
4.
FOR Index ← 5 TO 1 ____ -1 OUTPUT StudentNames[Index] NEXT Index
5.
FOR Index ← 1 TO 5 ______ StudentNames[Index], Marks[Index] NEXT Index

Spot the Error

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

Question 1
A OUTPUT "Class list"
B FOR Index ← 1 TO 5
C OUTPUT StudentNames[Index]
D NEXT Count
Question 2
A OUTPUT "Marks"
B FOR I ← 1 TO 4
C OUTPUT "Mark number ", I
D OUTPUT Marks[1]
E NEXT I
Question 3
A FOR Index 1 TO 5
B OUTPUT Pets[Index]
C NEXT Index
Question 4
A OUTPUT "Players"
B FOR Index ← 0 TO 3
C OUTPUT "Players[Index]"
D NEXT Index
Question 5
A FOR Index ← 1 TO 6
B OUTPUT Colours(Index)
C NEXT Index

Answer Key

Displaying every element with OUTPUT in a loop
Section A: Quick Questions
Q1
A — OUTPUT StudentNames[Index]
Q2
B — FOR Index ← 1 TO 30
Q3
B — True
Q4
A — OUTPUT "Mark ", Index, ": ", Marks[Index]
Q5
A — FOR Index ← 10 TO 1 STEP -1
Section B: Match the Pairs
Q1
OUTPUT StudentNames[Index] → Displays the element at the current index. Inside a FOR loop it outputs every element in order.
Q2
OUTPUT "Mark ", Index, ": ", Marks[Index] → Displays a label, the index number and the element, all on one line, separated by commas.
Q3
To display every element of an array in reverse order, the loop counts down using STEP -1. → FOR Index ← 10 TO 1 STEP -1 OUTPUT Marks[Index] NEXT Index
Q4
To display every element of a 5-element array in order, loop from the lower bound to the upper bound and output Names[Index]. → FOR Index ← 1 TO 5 OUTPUT Names[Index] NEXT Index
Q5
FOR Index ← 1 TO 5 OUTPUT StudentNames NEXT Index → The array name is used without an index, so it does not output one element per pass. It should be StudentNames[Index].
Q6
FOR Index ← 1 TO 5 OUTPUT Marks[5] NEXT Index → A fixed index of 5 is used instead of the loop variable, so the same element is output every time. It should be Marks[Index].
Section C: Definitions
Q1
OUTPUT StudentNames[Index] — Inside a FOR loop, this line displays every element of the array in order. Index changes on each pass, so each pass outputs the next element, from the lower bound to the upper bound.
Section D: Fill in the Blanks
Q1
FOR Index ← 1 TO 5 OUTPUT StudentNames[Index] NEXT Index
Q2
FOR Index ← 1 TO 5 OUTPUT StudentNames[Index] NEXT Index
Q3
FOR Index ← 1 TO 10 OUTPUT "Mark ", Index, ": ", Marks[Index] NEXT Index
Q4
FOR Index ← 5 TO 1 STEP -1 OUTPUT StudentNames[Index] NEXT Index
Q5
FOR Index ← 1 TO 5 OUTPUT StudentNames[Index], Marks[Index] NEXT Index
Section E: Spot the Error
Q1
D — NEXT must name the same loop variable as the FOR line. The loop counts with Index, so the last line should be NEXT Index.
Q2
D — A fixed index of 1 outputs the first element on every pass. The loop variable must be the index, so the line should be OUTPUT Marks[I].
Q3
A — The FOR line needs the assignment arrow to give the counter its starting value. It should be FOR Index ← 1 TO 5.
Q4
C — Quote marks make OUTPUT display the text Players[Index] literally. Without quotes it outputs the element, so it should be OUTPUT Players[Index].
Q5
B — Array elements are accessed with square brackets, not round brackets. The line should be OUTPUT Colours[Index].