← VCE Software Development U3O1

Looping through an array by index

Loops i through every index of arr, so arr[i] can be read or changed and you know each element's position. Used for totals, finding the largest value and editing in place.

Understanding Looping through an array by index in VCE Software Development U3O1

What it does: The total starts at 0 and each element, read by its index, is added in turn, giving the sum of the array. largest holds the biggest value seen so far and is updated whenever arr[i] beats it, so it ends as the maximum. range() needs a number. len(scores) gives the number of elements, so range(len(scores)) produces the indexes 0 to len-1. arr[i] is the element at position i. Using i alone would add up the index numbers, not the values. Assigning to arr[i] changes the list itself. Changing the loop variable item only changes a copy of the value, so the list stays the same.

Common mistakes: A running total must start at 0. Starting at 1 adds an extra 1 to the answer. It should be total = 0. Array elements are read with square brackets, not round ones. Round brackets try to call marks as a function. It should be biggest = marks[0].

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 72–96) and word search (Modules 72–113).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which loop visits every index of the list scores?
for i in range(scores):for i in range(len(scores)):
Matches
for i in range(len(arr)): total = total + i
↓
Adds the index i instead of the element arr[i], so the total is of positions, not values. It should be total = total + arr[i].
Applying
Meanings
Term
for i in range(len(arr)):
Meaning
Loops i through every index of arr, so arr[i] can be read or changed and you know each element's position. Used for totals, finding the largest value and editing in place.
Practice
marks = [72, 85, 64, 91, 78]
Testing
Gaps
for i in (len(arr)):
Check Answer
Spots
1scores = [12, 8, 15, 9]
2total = 1

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Barreleye Fish' badge — Clear Vision, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which loop visits every index of the list scores?
A. for i in range(scores):
B. for i in range(len(scores)): ✓
range() needs a number. len(scores) gives the number of elements, so range(len(scores)) produces the indexes 0 to len-1.
2. A list arr has 5 elements. Which values does i take in: for i in range(len(arr)):
A. 0, 1, 2, 3, 4 ✓
B. 1, 2, 3, 4, 5
Indexes start at 0, so a list of 5 elements has indexes 0 to 4. range(5) stops before 5.
3. Which code adds up every element of arr into total?
A. for i in range(len(arr)): total = total + i
B. for i in range(len(arr)): total = total + arr[i] ✓
arr[i] is the element at position i. Using i alone would add up the index numbers, not the values.
4. Which line finds the largest element so far inside the loop?
A. if arr[i] > largest: ✓
B. if i > largest:
Compare the element arr[i] with largest. The index i is just a position, not a value in the array.
5. Which code doubles every element of arr in place?
A. for i in range(len(arr)): arr[i] = arr[i] * 2 ✓
B. for item in arr: item = item * 2
Assigning to arr[i] changes the list itself. Changing the loop variable item only changes a copy of the value, so the list stays the same.

Match the Pairs

for i in range(len(arr)): → Loops i through every index of arr, from 0 to the last position
arr[i] = arr[i] * 2 → Changes the element at position i in place by doubling it
Totalling array elements: start a total at 0, then add arr[i] on every pass → total = 0 for i in range(len(arr)): total = total + arr[i]
Finding the largest element: start with the first element and replace it whenever arr[i] is bigger → largest = arr[0] for i in range(len(arr)): if arr[i] > largest: largest = arr[i]
for i in range(len(arr)): total = total + i → Adds the index i instead of the element arr[i], so the total is of positions, not values. It should be total = total + arr[i].
for i in range(arr): arr[i] = arr[i] + 1 → range() needs a number, but arr is a list. It should be range(len(arr)) so i covers every index.

Key Term

for i in range(len(arr)):
Loops i through every index of arr, so arr[i] can be read or changed and you know each element's position. Used for totals, finding the largest value and editing in place.

Fill in the Blanks

1. for i in range(len(arr)):
Hint: Type the function that generates the index numbers to loop through.
2. total = 0 for i in range(len(arr)): total = total + arr[i]
Hint: Type the function that gives the number of elements in the array.
3. largest = arr[0] for i in range(len(arr)): if arr[i] > largest: largest = arr[i]
Hint: Type the comparison operator that checks whether the current element is bigger than the largest so far.
4. for i in range(len(arr)): arr[i] = arr[i] * 2
Hint: Type the loop variable that selects which position is being changed in place.
5. total = 0 for i in range(len(arr)): total += arr[i]
Hint: Type the operator that adds each element onto the running total.

Spot the Error

Question 1
A scores = [12, 8, 15, 9]
B total = 1
C for i in range(len(scores)):
D total = total + scores[i]
E print(total)
Line B — A running total must start at 0. Starting at 1 adds an extra 1 to the answer. It should be total = 0.
Question 2
A marks = [56, 91, 73, 84]
B biggest = marks(0)
C for i in range(len(marks)):
D if marks[i] > biggest:
E biggest = marks[i]
Line B — Array elements are read with square brackets, not round ones. Round brackets try to call marks as a function. It should be biggest = marks[0].
Question 3
A prices = [10, 20, 30]
B for i in range(prices):
C prices[i] = prices[i] * 2
D print(prices)
Line B — range() needs a whole number, but prices is a list. Use len(prices) to get the number of elements: for i in range(len(prices)):
Question 4
A temps = [21, 25, 19, 30]
B total = 0
C for i in range(len(temps)):
D total = total + i
E average = total / len(temps)
Line D — i is the index (position), not the element. Adding i totals 0, 1, 2, 3 instead of the temperatures. It should be total = total + temps[i].
Question 5
A nums = [4, 9, 2, 7]
B for i in range(len(nums)):
C nums = nums[i] + 1
D print(nums)
Line C — This replaces the whole array with a single number. To change one element in place, assign to nums[i]. It should be nums[i] = nums[i] + 1.

Student with a class code? Join your class →