← IGCSE Computer Science Paper 2

Swapping two values with a temporary variable

Saves the value of Marks[Index] in Temp so it is not lost. Marks[Index] can then be overwritten, and Temp is copied into Marks[Index + 1] to finish the swap.

Understanding Swapping two values with a temporary variable in IGCSE Computer Science Paper 2

What it does: After Temp has saved Marks[Index], that element can be overwritten with the value from Marks[Index + 1] without losing data. Save the first value in Temp, copy the second into the first, then copy Temp into the second. Each value ends up in the other position.

Common mistakes: Temp must save Marks[Index], the value that is about to be overwritten. Saving Marks[Index+1] means the original Marks[Index] is lost. It should be Temp ← Marks[Index]. The second element is overwritten with Marks[1] when it should be Marks[1] that is overwritten with Marks[2]. Both elements would end up as 40. It should be Marks[1] ← Marks[2].

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 296–306) and word search (Modules 296–316).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which set of lines correctly swaps Marks[Index] and Marks[Index + 1]?
Temp ← Marks[Index] Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← TempMarks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Marks[Index]
Matches
Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Marks[Index]
↓
There is no temporary variable. After the first line both elements hold the same value, because the original Marks[Index] was overwritten and lost.
Applying
Meanings
Term
Temp ← Marks[Index]
Meaning
Saves the value of Marks[Index] in Temp so it is not lost. Marks[Index] can then be overwritten, and Temp is copied into Marks[Index + 1] to finish the swap.
Practice
Temp ← Marks[Index]
Testing
Gaps
← Marks[Index]
Check Answer
Spots
1Temp ← Marks[Index+1]
2Marks[Index] ← Marks[Index+1]

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

The 'Giant Isopod' badge — Patient Builder, 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 set of lines correctly swaps Marks[Index] and Marks[Index + 1]?
A. Temp ← Marks[Index] Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Temp ✓
B. Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Marks[Index]
The correct version saves one value in Temp first. Without Temp, the first assignment overwrites a value that is then lost.
2. Why do we store a value in Temp before swapping two array elements?
A. So the first value is not lost when it is overwritten ✓
B. So the array becomes sorted automatically
Temp only keeps a copy of one value safe. Sorting comes from the comparisons and repeated swaps in the algorithm, not from Temp.
3. Marks[3] is 40 and Marks[4] is 70. You run Marks[3] ← Marks[4] and then Marks[4] ← Marks[3]. What are the values now?
A. Both are 70 ✓
B. Marks[3] is 70 and Marks[4] is 40
After the first line, the 40 is overwritten by 70 and is gone. The second line then copies 70 back, so both elements hold 70.
4. Which line should come first in a swap of Marks[Index] and Marks[Index + 1]?
A. Marks[Index + 1] ← Temp
B. Temp ← Marks[Index] ✓
Temp must be given a value before it can be copied anywhere. Saving Marks[Index] in Temp is always the first step.
5. What does the line Marks[Index + 1] ← Temp do at the end of a swap?
A. Saves the value of Marks[Index + 1] into Temp
B. Puts the saved original value of Marks[Index] into Marks[Index + 1] ✓
Temp was filled in the first line. The last line copies it out into the second position, completing the swap.

Match the Pairs

Temp ← Marks[Index] → Saves the value in Marks[Index] in Temp so it is not lost when that element is overwritten
Marks[Index + 1] ← Temp → Copies the saved value from Temp into the next element, finishing the swap
The middle step of a swap copies the next element into the current one, which is safe because the old value is already in Temp → Marks[Index] ← Marks[Index + 1]
A complete swap of two neighbouring array elements uses three assignments in the correct order → Temp ← Marks[Index] Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Temp
Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Marks[Index] → There is no temporary variable. After the first line both elements hold the same value, because the original Marks[Index] was overwritten and lost.
Marks[Index] ← Marks[Index + 1] Temp ← Marks[Index] Marks[Index + 1] ← Temp → Temp is assigned after Marks[Index] has already been overwritten, so Temp saves the wrong value and the original is lost.

Key Term

Temp ← Marks[Index]
Saves the value of Marks[Index] in Temp so it is not lost. Marks[Index] can then be overwritten, and Temp is copied into Marks[Index + 1] to finish the swap.

Fill in the Blanks

1. Temp ← Marks[Index] Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Temp
Hint: Type the name of the temporary variable that saves the first value before it is overwritten.
2. Temp ← Marks[Index] Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Temp
Hint: Type the loop variable that points to the first of the two elements being swapped.
3. Temp ← Marks[Index] Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Temp
Hint: Type the variable that holds the saved value, so it can finish the swap.
4. Temp ← Marks[Index] Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Temp
Hint: Type the assignment symbol that copies the array value into the temporary variable.
5. Temp ← Marks[Index] Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Temp
Hint: Type the index expression for the next element along, which is copied into the first position.

Spot the Error

Question 1
A Temp ← Marks[Index+1]
B Marks[Index] ← Marks[Index+1]
C Marks[Index+1] ← Temp
Line A — Temp must save Marks[Index], the value that is about to be overwritten. Saving Marks[Index+1] means the original Marks[Index] is lost. It should be Temp ← Marks[Index].
Question 2
A Marks[1] ← 40
B Marks[2] ← 75
C Temp ← Marks[1]
D Marks[2] ← Marks[1]
E Marks[2] ← Temp
Line D — The second element is overwritten with Marks[1] when it should be Marks[1] that is overwritten with Marks[2]. Both elements would end up as 40. It should be Marks[1] ← Marks[2].
Question 3
A Temp ← Marks[Index]
B Marks[Index] ← Marks[Index+1]
C Marks[Index+1] ← Marks[Index]
Line C — Marks[Index] has already been overwritten on line 2, so copying it again gives the same value twice. The original value is in Temp. It should be Marks[Index+1] ← Temp.
Question 4
A DECLARE Temp : INTEGER
B Temp ← Marks[3]
C Marks[3] = Marks[4]
D Marks[4] ← Temp
Line C — The = sign is used for comparison, not for assignment. Cambridge pseudocode uses ← to store a value. It should be Marks[3] ← Marks[4].
Question 5
A IF Marks[1] > Marks[2] THEN
B Marks[1] ← Temp
C Marks[1] ← Marks[2]
D Marks[2] ← Temp
E ENDIF
Line B — Temp has not been given a value yet, so this overwrites Marks[1] and loses it. The first step must be to save Marks[1] in Temp. It should be Temp ← Marks[1].

Student with a class code? Join your class →