DoAssignment study guide

C3.1 · Write efficient code using counts, subprograms, and control structures

Learn to write efficient code using counts, subprograms, and control structures through clear examples and targeted practice.

Ontario Grade 7 Mathematics

Algebra

Plan clear instructions by spotting repeated actions and organizing them well

Code is a set of instructions that a computer or a coding device follows. You may already know how to put instructions in order. For example, to draw a square with a robot, it could move forward and turn four times. If the same pair of instructions is written four times, the code is long and harder to change. A count can show how many times to repeat them. A subprogram can give that pair a useful name. These tools help make code shorter and easier to understand while keeping the result the same.

What you will learn

  • Describe what code instructions do in a simple sequence.
  • Use a count to repeat an instruction a chosen number of times.
  • Use a subprogram to give a reusable group of instructions a name.
  • Choose among sequence, repetition, and choice to make code clearer and more efficient.
  • Check that code gives the intended result.

1. Start with clear instructions

A program is a set of code instructions that completes a task. An instruction tells the device to do one action, such as move forward, turn, or show a message. A sequence is a group of instructions in the order they happen. The order matters: turning before moving can make a different path from moving before turning.
Before writing code, say the task in everyday words. For a square, the plan is: move forward, turn a quarter-turn, and repeat that pair four times. A quarter-turn means turning one of four equal parts of a full turn. We can use a count to show the number of repetitions.
Efficient code does the intended task without unnecessary repeated writing. Efficient does not mean using the fewest possible letters at any cost. The code must still be clear enough for a person to understand and check.
  • A sequence runs instructions in order.
  • A count tells how many times an action or group of actions repeats.
  • Good code is both clear and suited to its task.

2. Use counts for repeated actions

Suppose a robot draws a square. It must move the same distance along each side, then turn the same amount at each corner. Writing the two actions four times works, but it repeats the same code. A repeat instruction with a count can express the same plan more simply.
A control structure is a code pattern that controls what instructions happen and when. Repetition is one kind of control structure. It tells the device to run an instruction or group of instructions again. The count sets the number of times.
To read a repeat instruction, identify the count and the actions inside it. If the count is four, the actions inside run four times. Actions outside the repeat happen only where they are placed in the sequence. A count should match the task: too few repetitions leave the task unfinished; too many may add unwanted actions.
repeat n times: actions\text{repeat } n \text{ times: actions}
  • A repeat structure saves rewriting when actions happen again in the same way.
  • Check that the count matches the number of repetitions needed.
  • The actions inside the repeat are the actions that happen again.

3. Give reusable instructions a name

A subprogram is a named group of instructions that can be used as part of a larger program. Think of it as a small recipe with a name. For example, a group that moves forward and turns can be named “draw one side and corner.” The main code can then use that name four times.
A subprogram is useful when a group of actions is needed more than once, or when a task becomes easier to understand as smaller named parts. The name should describe what the instructions do. A name such as “thing” does not help much; “draw one side and corner” gives a clue.
A subprogram does not automatically repeat just because it has a name. The main code must tell the device to use it. The repeat count controls how many times the named instructions are used. This separates two ideas: a subprogram stores a useful group of instructions, while a repeat structure controls how often a group runs.
repeat \text{repeat } 4 times: draw one side and corner
  • A subprogram is a reusable, named group of instructions.
  • Use a clear name that describes the group’s job.
  • A repeat count can tell the program how many times to use a subprogram.

4. Choose the control structure that fits

Three useful code patterns are sequence, repetition, and choice. A sequence runs instructions in order. Repetition runs instructions again, often using a count. Choice selects what to do based on a condition, which is a rule that can be true or false. For example, a simple game might say, “If the answer is correct, show ‘Well done’; otherwise, try again.”
Use a sequence when actions happen once in a set order. Use repetition when the same action or group happens several times. Use choice when the program needs to respond differently depending on a condition. A program can use more than one pattern. For example, it can first check a choice and then repeat a group of actions.
To make code efficient, look for repeated instructions and consider whether a count or subprogram would make their purpose clearer. Then read the code in order. Ask: What happens first? Which actions repeat? What decides a choice? Finally, compare the result with the task. Shorter code is not better if it changes the intended result or hides what the code does.
  • Sequence means instructions run in order.
  • Repetition means instructions run again.
  • Choice means the program follows a path based on a condition.
  • Test the code by tracing its instructions and checking the result.

Three ways to organize instructions

Code patternWhat it doesWhen it fits
SequenceRuns instructions in orderActions happen once in a set order
RepetitionRuns instructions againAn action or group is needed several times
ChoiceSelects an action based on a conditionThe program needs different responses
SubprogramNames a reusable group of instructionsA group needs a clear name or is used again

Worked example

Draw a square with a count

A drawing robot starts at a corner. It can move forward one side length and turn a quarter-turn. Write a short plan for drawing a square. Use a repeat count.
  1. Name the repeated actions
    Each side uses the same two actions: move forward one side length, then turn a quarter-turn. A square has four sides, so this pair is needed four times.
  2. Use a count
    Put the repeated pair inside a repeat instruction. The count is four because the robot must draw four sides and make four turns to return to its starting direction.
    repeat 4 times: move forward; turn a quarter-turn\text{repeat } 4 \text{ times: move forward; turn a quarter-turn}
Answer: Repeat four times: move forward one side length, then turn a quarter-turn.
Check: The code has two actions inside the repeat and a count of four. That produces four equal sides and four equal turns.

Worked example

Use a subprogram in a larger plan

A robot needs to draw three identical marks. Each mark uses this group: move forward, turn, move forward again, then return to the starting direction. Describe how a subprogram and a count can organize the code.
  1. Make a named group
    Put the four actions for one mark in a subprogram. A name such as “draw one mark” tells the reader what the group does.
    subprogram: draw one mark\text{subprogram: draw one mark}
  2. Use the group three times
    The marks are identical, so the main code can repeat the named subprogram three times. The subprogram keeps the details together, and the count shows how many marks to draw.
    repeat 3 times: draw one mark\text{repeat } 3 \text{ times: draw one mark}
Answer: Create a subprogram called “draw one mark” with the four actions. In the main code, repeat “draw one mark” three times.
Check: There are three marks, so the count is three. The actions for one mark appear in one named group rather than being copied three times.

Worked example

Select a choice based on a condition

A classroom program checks whether a learner’s answer matches the target answer. Describe a simple choice structure for what the program should show.
  1. State the condition
    The condition is whether the learner’s answer matches the target answer. It can be true or false.
  2. Give an action for each result
    If the condition is true, show a success message. Otherwise, show a try-again message. This is choice because the program selects an action based on the condition.
    if correct: show success; otherwise: show try again\text{if correct: show success; otherwise: show try again}
Answer: If the answer is correct, show “Correct.” Otherwise, show “Try again.”
Check: The program has one condition and a different response for each result. It does not need a repeat count because the task asks for a choice, not repeated actions.

Common mistakes and how to avoid them

Using a repeat count that does not match the task.
Correction: Count how many times the action or group must happen, then use that number.
Thinking a subprogram repeats automatically because it has a name.
Correction: The main code must use the subprogram. Add a repeat count if it must run several times.
Putting an action outside the repeat when it should happen every time.
Correction: Place all actions that must happen on each repetition inside the repeated group.
Using repetition when the program needs to choose between different responses.
Correction: Use a choice structure when the next action depends on a condition.

Lesson summary

  • A sequence runs instructions in order.
  • A count tells how many times instructions repeat.
  • A subprogram is a named group of instructions that can be used in a larger program.
  • Control structures organize when instructions run: sequence, repetition, and choice.
  • Trace the instructions and check that the result matches the task.

Check your understanding

Question 1

A robot must repeat the same group of actions six times. Which code idea shows how many repetitions are needed?
  1. A count of six
  2. A subprogram name only
  3. A choice with no condition
  4. A sequence with no repeated actions
Show answer and explanation
A count of six
A count tells how many times the group should repeat.

Question 2

A named group contains the steps for drawing one star. What is the named group?
  1. A subprogram
  2. A count
  3. A condition
  4. A sequence that must happen only once
Show answer and explanation
A subprogram
A subprogram is a named group of instructions that can be used in a larger program.

Question 3

A program shows one message when an answer is correct and another when it is not. Which control structure fits?
  1. Choice
  2. A repeat count
  3. A subprogram name without instructions
  4. A sequence with no condition
Show answer and explanation
Choice
The program selects a response based on whether the condition is true or false.

Key terms

Code
Instructions that tell a computer or coding device what to do.
Sequence
Instructions that run in a set order.
Count
A number that tells how many times an action or group of actions repeats.
Control structure
A code pattern that controls what instructions happen and when.
Repetition
A control structure that runs an instruction or group again.
Subprogram
A named group of instructions that can be used as part of a larger program.
Condition
A rule that can be true or false and can guide a choice in code.
Efficient code
Code that completes the task clearly without unnecessary repeated writing.

Continue through Grade 7 Math

View the complete Grade 7 Math Ontario Grade 7 Mathematics curriculum and lessons

About this lesson and its review

Published by DoAssignment. This reviewed lesson follows Ontario Grade 7 Mathematics, expectation C3.1. 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.

Official curriculum reference

Report a correction or ask a question