DoAssignment study guide
C3.2 · Change code and explain effects on output and efficiency
Learn to change code and explain effects on output and efficiency through clear examples and targeted practice.
Ontario Grade 6 Mathematics
Algebra
See how a small code change can change a result or the way a program is written
Code is a set of instructions that a computer follows. Output is what the code produces, such as a number or a list of numbers. When you change code, the output may change. Code can also be written in different ways to make the same output. In this lesson, you will trace short instructions, change one part at a time, and explain what happened. The examples use invented code and values for learning. They are not measurements from the real world.
What you will learn
- Describe the output of a short set of code instructions.
- Change one part of code and explain how the output changes.
- Compare two pieces of code that make the same output and explain which is more efficient to write.
1. Trace instructions to find the output
Before changing code, find out what the original code does. Read each instruction in order. Keep track of the value after every instruction. This is called tracing the code. A trace is a record of the steps and results.
Suppose a program starts with the number 2. It adds 3, then adds 3 again. The output is 8. The starting number is not the output by itself; follow all the instructions to get the final result.
A repeat instruction tells the computer to do another instruction a set number of times. For example, “repeat 3 times: add 2” means add 2 three times. The repeated instruction is still carried out each time. Counting carefully helps prevent missed or extra actions.
- Output is the result produced by running code.
- Trace code in the order its instructions are given.
- A repeat instruction carries out its inner instruction the stated number of times.
2. Change code and explain what changes
To study the effect of a code change, first note the original output. Then change one part of the code and trace it again. Comparing the two outputs helps you say exactly what changed.
For example, changing “repeat 3 times” to “repeat 4 times” adds one more round of the repeated instruction. If the instruction adds 2, the new result is 2 more than the old result. Changing the number being added would have a different effect. Be clear about which part you changed.
A helpful explanation names the edit and its effect: “I changed the repeat count from 3 to 4. The program adds 2 one extra time, so the output increases by 2.” This connects the code change to the result.
- Compare the original code and changed code.
- Change one part at a time when you want to identify its effect.
- Explain why the output changed by referring to the instruction that changed.
3. Compare how efficiently code is written
Efficiency means doing a task without unnecessary work. For this lesson, compare how many instruction lines a person needs to write to make the same output. This is a simple way to discuss code efficiency. It does not mean that shorter code is always better; the code must still be clear and produce the intended result.
If an instruction needs to happen many times, a repeat instruction can avoid writing the same instruction on many separate lines. The computer still carries out each repetition. The repeat instruction can make the written code more compact, while the output stays the same.
When comparing code, check two things. First, do both versions produce the same output? Second, does one version avoid writing the same instruction again and again? If both answers are yes, explain that the repeated version is more efficient to write because it uses a repeat instruction.
- Efficiency here is about avoiding unnecessary repeated writing.
- A repeat instruction can make code shorter while keeping the same output.
- Always check that the code still gives the intended result.
4. A reliable way to review a code change
Use this routine when you examine or edit short code. First, read the instructions and identify the output. Next, trace the code by keeping a running value or listing each result. Then make the requested change and trace the new code. Finally, compare the outputs and explain the effect in a full sentence.
For an efficiency comparison, trace both versions before deciding. A shorter-looking version is not automatically correct. Confirm that it performs the same actions the needed number of times and produces the same result.
Practise by predicting an output before checking it. If your prediction is different, retrace one instruction at a time. This makes it easier to find whether you missed an instruction or counted a repetition incorrectly.
- Trace first, edit second, and compare outputs last.
- For efficiency, compare the written instructions and confirm that the results match.
- Use a clear sentence to link the code edit to its effect.
Worked example
Example 1: Change the number of repetitions
A program starts at 1 and repeats “add 4” three times. Change the repeat count to four. Find both outputs and explain the effect of the change.
- Trace the original codeStart at 1. Add 4 three times, keeping the running value after each addition.
- Trace the changed codeThe changed code adds 4 one extra time. Continue from the original result and add 4 once more.
- Explain the effectThe repeat count increased from three to four. The program added one extra group of 4, so the output increased from 13 to 17.
Answer: The original output is 13. The changed output is 17. Increasing the repeat count by one makes the program add 4 one extra time, so the output increases by 4.
Check: There are four additions in the changed code, not three. Starting at 1 and adding 4 four times gives 17.
Worked example
Example 2: Change the instruction inside the repeat
A program starts at 0 and repeats “add 3” four times. Change the instruction to “add 5” but keep the repeat count at four. Find each output and explain the effect.
- Trace the original codeThe program adds 3 four times. Starting at 0, record each new value until all four additions are done.
- Trace the changed codeNow each repetition adds 5 instead. The program still repeats four times, so use four additions of 5.
- Compare the outputsThe repeat count stayed the same, but each addition became 2 larger. Across four repetitions, the changed program produces an output 8 larger.
Answer: The original output is 12 and the changed output is 20. Changing the repeated addition from 3 to 5 makes each of four additions 2 larger, so the final output is 8 larger.
Check: The program still repeats four times. The changed instruction is “add 5,” so the output is 20.
Worked example
Example 3: Use repetition to avoid repeated writing
Two programs start at 0 and produce the same result. Program A has four separate instructions: “add 2,” “add 2,” “add 2,” and “add 2.” Program B says “repeat 4 times: add 2.” Compare the outputs and explain which is more efficient to write.
- Find Program A's outputProgram A adds 2 four times. Starting at 0, its running total ends at 8.
- Find Program B's outputProgram B also carries out “add 2” four times. It therefore gives the same output.
- Compare the written codeBoth programs do the same task and give the same output. Program B is more efficient to write because one repeat instruction replaces four separate copies of the same instruction.
Answer: Both programs produce 8. Program B is more efficient to write because it uses a repeat instruction instead of writing “add 2” four times. The computer still performs four additions.
Check: The repeat instruction does not mean “add 2 once.” It means carry out that instruction four times.
Common mistakes and how to avoid them
Counting the starting value as one of the repeated additions.
Correction: The starting value is there before the repeats begin. Count only how many times the instruction inside the repeat is carried out.
Changing both the instruction and the repeat count, then claiming one change caused the whole effect.
Correction: Change one part at a time when you are investigating its effect. Trace the code after each change.
Saying that a repeat instruction makes the computer do less work.
Correction: The computer still carries out every repetition. The code is more efficient to write because the same instruction does not need to be written on separate lines.
Assuming shorter code must produce the same output.
Correction: Trace both versions and check that they carry out the same instruction the same number of times.
Lesson summary
- Output is the result produced by running code.
- Trace instructions in order and count repeat instructions carefully.
- Change one part of code, trace it again, and explain how that edit affects the output.
- A repeat instruction can make code more efficient to write when it replaces repeated copies and keeps the same output.
Check your understanding
Question 1
A program starts at 2 and repeats “add 3” three times. What is its output?
- 8
- 9
- 11
- 12
Show answer and explanation
11
Start at 2, then add 3 three times: 5, 8, 11. The output is 11.
Question 2
A program starts at 0 and repeats “add 4” twice. If the repeat count changes to three, what happens to the output?
- It stays 8 because only the repeat count changed.
- It increases by 4, from 8 to 12.
- It increases by 3, from 8 to 11.
- It becomes 3 because the new repeat count is 3.
Show answer and explanation
It increases by 4, from 8 to 12.
The new code adds 4 one extra time. The output changes from 8 to 12, an increase of 4.
Question 3
Which statement best explains why “repeat 5 times: add 1” can be more efficient to write than five separate “add 1” instructions?
- It avoids writing the same instruction separately, while still carrying it out five times.
- It makes the output five times smaller.
- It carries out the instruction only once.
- It removes the need to check the output.
Show answer and explanation
It avoids writing the same instruction separately, while still carrying it out five times.
The repeat instruction replaces repeated writing. The computer still carries out “add 1” five times.
Key terms
- Code
- Instructions written so a computer can carry them out.
- Output
- The result produced when code runs.
- Trace
- A record of what happens as each instruction is followed.
- Repeat instruction
- An instruction that tells a computer to carry out another instruction a set number of times.
- Efficiency
- Doing a task without unnecessary work. Here, it includes avoiding repeated writing when the same output can be made with a repeat instruction.
Continue through Grade 6 Math
View the complete Grade 6 Math Ontario Grade 6 Mathematics curriculum and lessons
- C3.1 · Write efficient code with conditionals and control structures
- D1.1 · Distinguish discrete from continuous data
- C1.1 · Identify repeating, growing, shrinking, and linear patterns
- C1.2 · Represent patterns with tables, graphs, and expressions
- C1.3 · Use pattern rules to predict and find missing terms
- C1.4 · Create patterns showing whole-number and decimal relationships
About this lesson and its review
Published by DoAssignment. This reviewed lesson follows Ontario Grade 6 Mathematics, expectation C3.2. It is a study resource, not an official curriculum publication.
Before publication, content is checked for structure, mathematical or chemical notation, calculations, course boundaries, and readability. Errors can still occur, so corrections are welcomed.