DoAssignment study guide
C3.2 · Modify code and explain changes to its output and efficiency
Learn to modify code and explain changes to its output and efficiency through clear examples and targeted practice.
Ontario Grade 7 Mathematics
Algebra
Modify a short program and explain its output and efficiency
Code is a set of instructions for a computer. A small change to an instruction can change what the computer displays. It can also change how much work the computer does. In this lesson, you will trace short programs, edit them, and explain both effects. You do not need to know a programming language. The examples use simple pseudocode: clear instructions written in a code-like form.
What you will learn
- Describe what a short piece of code does.
- Modify code to produce a different, intended output.
- Explain how a code change affects the output.
- Compare the efficiency of simple code by counting repeated steps.
Read code by tracing each instruction
Before changing code, find out what it already does. A trace is a record of the values and actions as the instructions run. Follow the instructions in order. When a value changes, update it in your trace.
A variable is a named place for a value. For example, a program might store a number in a variable called score. An output instruction displays information for the person using the program. A repeated instruction, such as a loop, runs its contents more than once.
Try this small program. It starts with a value, adds one three times, and displays the result.
count = 0
repeat 3 times:
count = count + 1
display count
repeat 3 times:
count = count + 1
display count
At the start, count is 0. After the first repeat it is 1. Then it is 2, and finally it is 3. The displayed output is 3. Tracing makes the result clear instead of relying on a guess.
- Read instructions in order.
- Track how each variable changes.
- The output is what the program displays.
Modify code to change its output
To modify code is to change one or more instructions. Make a change that matches the goal. Then trace the new version to check what it displays. Explaining the change means saying what was edited and how that edit caused the new result.
A condition is a check that can be true or false. For example, a program can check whether a number is greater than 5. An if instruction can display one message when a condition is true and another when it is false.
Changing a number in a condition may change which message appears. Changing the value being checked may do the same. Do not assume that a change works: trace the instructions with the new values.
A useful explanation names both versions. For example: “I changed the repeat count from 3 to 5. The addition now happens five times instead of three, so the displayed count increases from 3 to 5.”
- Start with the goal for the program.
- Change an instruction, then trace the changed code.
- Explain the connection between the edit and the new output.
Compare how much work code does
Efficiency describes how much work a program does to reach its result. For these short examples, compare the number of times an instruction runs. This is a simple way to discuss efficiency; it is not a measure of seconds on a particular device.
A loop that repeats an instruction 10 times does that instruction more times than a loop that repeats it 4 times. If both programs reach the same result, the one with fewer repeated actions may be more efficient for that task.
Sometimes a changed program does more work because it handles more items or gives more detail. That may be useful. Explain the trade-off plainly: state what the program now does and whether it repeats more or fewer steps.
Count the work that matters to the task. If a program checks each item in a list once, adding more items means more checks. If an instruction is outside a loop, it runs once; if it is inside the loop, it runs each time the loop repeats.
- For these examples, compare repeated actions by counting them.
- More repetitions mean more of that instruction's work.
- A useful change may do more work to produce a needed result.
A reliable way to explain a code change
First, state the goal. Next, read or trace the original code to find its output. Then make a small change and trace the changed code. Finally, compare the outputs and count any repeated actions that changed.
Keep your explanation specific. Instead of saying “the program is better,” say what it displays now and how many times the key instruction runs. If the program's output is unchanged, say so and explain what did change, such as the number of checks.
Before finishing, check that the edited code still follows the intended instructions. A change can produce the right output for one test but fail for another input. Try the values stated in the task, and trace them carefully.
- State the goal, trace, edit, trace again, and compare.
- Describe output and repeated work separately.
- Use the tested values to support your explanation.
Worked example
Change the number of additions
A program should display the total number of stars after adding one star in each repeat. The original repeat count is 4. Modify it so the program displays 6. Explain the output and the change in repeated work.
- Trace the originalThe variable starts at 0. The loop adds 1 four times, so the final value is 4. The program displays 4.
- Make the editChange the loop to repeat 6 times. Each repeat still adds one, so the value increases by six altogether.
- Compare the workThe changed program displays 6. The addition instruction runs 6 times instead of 4, which is two more additions.
Answer: Changed code: count = 0; repeat 6 times: count = count + 1; display count. It displays 6. The addition runs 6 times, which is 2 more times than before.
Check: Six additions of one starting from zero give a final count of six.
Worked example
Change a condition to include 5
This program displays “large” when a score is greater than 5 and “small” otherwise. Modify it so a score of 5 is also called “large.” Explain what happens for scores 5 and 6.
- Check the original conditionThe original check is score > 5. A score of 5 is not greater than 5, so it displays “small.” A score of 6 is greater than 5, so it displays “large.”
- Edit the conditionChange the check to score >= 5. The symbol >= means greater than or equal to. The revised condition includes 5 as well as numbers above 5.
- Trace both scoresFor a score of 5, the new condition is true, so the program displays “large.” For a score of 6, it is also true, so the program displays “large.” The program still checks the condition once for each score.
Answer: Use the condition score >= 5. A score of 5 and a score of 6 both produce “large.” The change affects the result for 5 but does not add another check.
Check: The inclusive condition covers 5 and values above 5; it does not require the program to repeat its check.
Worked example
Reduce repeated checks
A program checks each of 5 entries once, then displays “checked.” A changed version checks each entry twice before displaying the same message. Modify the changed version so each entry is checked once. Compare the output and the number of checks.
- Count the changed versionThere are 5 entries, and each is checked twice. That makes 10 checks. After the checks, the program displays “checked.”
- Remove the duplicate checkKeep one check for each entry and remove the extra pass through the entries. The program still reaches the display instruction.
- Compare versionsBoth versions display “checked.” The edited version checks 5 entries once, so it makes 5 checks instead of 10. It does fewer repeated actions while keeping the stated output.
Answer: Use one check for each of the 5 entries. The output remains “checked,” while the number of checks falls from 10 to 5.
Check: Each entry is counted once in the edited version, so there are 5 checks total.
Common mistakes and how to avoid them
Guessing the output without tracing the instructions.
Correction: Follow each instruction in order and update variable values as they change.
Saying a program is more efficient without counting the work.
Correction: Name the repeated instruction and compare how many times it runs.
Changing several instructions at once and losing track of the cause.
Correction: When possible, make one clear change, then trace and explain its effect.
Lesson summary
- Trace code to work out what it displays.
- Modify an instruction to meet a stated goal, then trace the new version.
- Explain how the edit changes the output.
- Compare simple programs by counting repeated actions.
Check your understanding
Question 1
A variable starts at 2. A loop adds 3 once each time it repeats 4 times. What does the program display?
- 5
- 12
- 14
- 20
Show answer and explanation
14
The four additions increase the starting value by 12. The final value is 14.
Question 2
A condition is changed from points > 8 to points >= 8. What is the main output change for a value of 8?
- The program now treats 8 as meeting the condition.
- The program checks the condition eight times.
- The program always treats values below 8 as meeting the condition.
- The program displays the number 8 no matter what the input is.
Show answer and explanation
The program now treats 8 as meeting the condition.
The revised condition includes 8 itself. It does not change the number of times the check runs.
Question 3
A program checks 7 entries twice each. It is changed to check each entry once. How many checks are removed?
- 2
- 7
- 9
- 14
Show answer and explanation
7
The original has 14 checks. The edited version has 7. The difference is 7 checks.
Key terms
- Code
- Instructions that tell a computer what to do.
- Trace
- A record of what happens as code instructions run.
- Variable
- A named place in a program for storing a value.
- Output
- Information a program displays or produces.
- Condition
- A check that can be true or false.
- Efficiency
- How much work a program does to reach its result.
Continue through Grade 7 Math
View the complete Grade 7 Math Ontario Grade 7 Mathematics curriculum and lessons
- C3.1 · Write efficient code using counts, subprograms, and control structures
- D1.1 · Explain percent distributions of large populations and samples
- C1.1 · Compare repeating, growing, and shrinking patterns by rate and starting value
- C1.2 · Represent number patterns with tables, expressions, and linear equations
- C1.3 · Extend patterns and use their rules to find missing values
- C2.1 · Add and subtract first-degree monomials using models
About this lesson and its review
Published by DoAssignment. This reviewed lesson follows Ontario Grade 7 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.