DoAssignment study guide

C3.1 · Write efficient code with conditionals and control structures

Learn to write efficient code with conditionals and control structures through clear examples and targeted practice.

Ontario Grade 6 Mathematics

Algebra

Grade 6 Mathematics — C3.1: Write efficient code with conditionals and control structures

Code is a set of instructions that a computer follows. You may already know how to put instructions in order. In this lesson, you will learn how code can choose between actions and repeat an action. A conditional is an instruction that checks whether something is true or false, then chooses what to do. A control structure is a way to organize instructions, such as choosing an action or repeating instructions. Efficient code does the needed job clearly without unnecessary repeated instructions. We will use simple, made-up examples. The numbers are inputs for the examples, not measured data.

What you will learn

  • Explain how a conditional makes a choice in code.
  • Use a repeat structure to avoid writing the same instruction many times.
  • Write and check short code that uses a condition and a control structure.

1. From a decision to a conditional

Imagine sorting a card by its number. The rule is: if the number is greater than 5, put the card in the “large” pile; otherwise, put it in the “small” pile. The word “otherwise” means what to do when the condition is not true.
A condition is a question that can have a true or false answer. In this example, the condition is “number is greater than 5.” The code checks the condition and follows one of two paths. A conditional is useful whenever an instruction depends on a condition.
In plain-language pseudocode—a simple way to describe code without requiring a particular coding app—the rule can be written like this: “If number is greater than 5, say ‘large’; otherwise, say ‘small’.” The two choices should cover the possible answers to the condition. Check carefully what happens when the number equals 5: it is not greater than 5, so it follows the “otherwise” path.
  • A condition has a true or false answer.
  • An if instruction chooses what to do when its condition is true.
  • An otherwise instruction gives an action for the other case.

2. Repeating instructions with control structures

Suppose a character must move forward four spaces. One way is to write “move forward” four times. A repeat structure tells the computer to do an instruction a set number of times. It can make the code shorter and easier to change.
The word repeat is a control structure. In pseudocode, “repeat 4 times: move forward” means to carry out the move instruction four times. The instruction inside the repeat structure is called the repeated instruction. The count tells how many times it is carried out.
Code should be clear, not just short. If a repeated instruction is hard to understand, use clear names and check the count. A repeat count of 4 means four moves, not the first move plus four more. Some coding tools use different words or blocks for repetition, but the idea is the same.
A conditional and a repeat structure can also be used together. For instance, a program could repeat a check for each card, then choose a pile for that card. The repeat structure controls how often the check happens. The conditional controls which action happens for a card.
  • A repeat structure carries out an instruction more than once.
  • A repeat count tells how many times the instruction runs.
  • A conditional chooses an action; a repeat structure controls repetition.

3. Plan, trace, and improve code

Before writing code, name the input and the action you want. An input is information given to a program, such as a number. Then state the rule in everyday words. This makes it easier to choose a condition and predict what the program should do.
To trace code means to follow its instructions in order and record what happens. For a conditional, check the condition using the input. For a repeat structure, count each time the repeated instruction runs. A small table or a set of input-and-output points can help you check that your code follows the rule.
When improving code, look for instructions that do the same job again and again. A repeat structure may replace those copies. Do not remove an instruction that changes the result. After revising, trace the code again with a few inputs, including one that makes the condition true and one that makes it false. This is a practical way to catch a wrong condition or an incorrect repeat count.
  • Tracing means following code in order and recording its actions.
  • Check both paths of a conditional and the number of repeats.
  • Efficient code avoids needless copies while keeping the intended result.

Worked example

Choose a message using a condition

A program receives a whole-number input called score. Write pseudocode that says “ready” when score is at least 6, and says “try again” otherwise. What message appears for inputs 4 and 8?
Score and message code
Score and message codeHypothetical input-output examples for the stated rule. The vertical numbers stand for the two messages: 0 means “try again” and 1 means “ready.”score inputmessage code: 0 or 14, try again8, readyHypothetical code examples

Hypothetical input-output examples for the stated rule. The vertical numbers stand for the two messages: 0 means “try again” and 1 means “ready.”

  1. State the condition
    The program needs to test whether score is at least 6. “At least” includes 6 itself, so use a condition that includes equality.
    score≥6score ≥ 6
  2. Write both choices
    If the condition is true, the program says “ready.” Otherwise, it says “try again.” The otherwise action covers inputs that are less than 6.
  3. Trace the inputs
    For input 4, the condition is false, so the message is “try again.” For input 8, it is true, so the message is “ready.”
    4<6,8≥64 < 6, 8 ≥ 6
Answer: Pseudocode: “If score is at least 6, say ‘ready’; otherwise, say ‘try again’.” Input 4 gives “try again.” Input 8 gives “ready.”
Check: The input 6 also gives “ready,” because “at least 6” includes 6.

Worked example

Repeat a movement instruction

A character starts in place. Write pseudocode for moving forward exactly 5 spaces without writing the same movement instruction five times. How many movement instructions run?
  1. Find the repeated action
    The action that happens again is “move forward.” Put that instruction inside a repeat structure.
  2. Set the repeat count
    The character must move exactly 5 spaces, so the instruction runs 5 times. The repeat count is the number of times the instruction runs.
    5 moves5\text{ moves}
  3. Check the result
    Count one movement each time through the repeat structure. Five repetitions produce five movements, not six.
    1+1+1+1+1=51+1+1+1+1=5
Answer: Pseudocode: “Repeat 5 times: move forward.” The movement instruction runs 5 times.
Check: The repeat structure replaces five separate copies of the same instruction while keeping the required number of moves.

Worked example

Use a condition inside a repeat

A program checks each of three inputs, one at a time. For each input, it says “match” if the input is 2 and “not a match” otherwise. Write pseudocode and find the messages for the hypothetical inputs 2, 5, and 2.
Input and message code
Input and message codeHypothetical inputs for the repeated conditional. The vertical numbers stand for messages: 1 means “match” and 0 means “not a match.”input turnmessage code: 0 or 1turn 1: matchturn 2: not a matchturn 3: matchHypothetical code example

Hypothetical inputs for the repeated conditional. The vertical numbers stand for messages: 1 means “match” and 0 means “not a match.”

  1. Place the check inside the repeat
    The program must handle three inputs, so use a repeat structure for three turns. During each turn, check the current input with a conditional.
  2. Write the rule clearly
    For each input, the condition is “current input equals 2.” If true, say “match”; otherwise, say “not a match.”
    current input=2\text{current input}=2
  3. Trace each turn
    The first input is 2, so it matches. The second is 5, so it does not match. The third is 2, so it matches. The repeat structure handles each input once. 2\tomatch, 5\tonot a match, 2\tomatch
Answer: Pseudocode: “Repeat 3 times: check the next input. If it equals 2, say ‘match’; otherwise, say ‘not a match’.” The messages are “match,” “not a match,” and “match.”
Check: There are three inputs and three checks. The conditional makes a separate choice for each input.

Common mistakes and how to avoid them

Using “greater than 6” when the rule says “at least 6.”
Correction: “At least 6” includes 6. Use a condition that means greater than or equal to 6.
Leaving out the otherwise action when the program needs to handle both possible answers.
Correction: Add an otherwise action for when the condition is false.
Repeating an instruction one time too many.
Correction: Trace the repeat count. A count of 5 means the instruction runs five times in total.
Putting a conditional outside a repeat when each input needs its own check.
Correction: Put the check inside the repeat so the program makes a choice for every input.

Lesson summary

  • A conditional checks a condition and chooses an action.
  • A repeat structure runs an instruction a set number of times.
  • Trace code in order to check its choices and repeat count.
  • Efficient code avoids needless repeated instructions without changing the result.

Check your understanding

Question 1

A program says “yes” if number equals 3 and “no” otherwise. What does it say for input 4?
  1. yes
  2. no
  3. It repeats the check four times.
  4. There is not enough information.
Show answer and explanation
no
The condition is false for 4, so the otherwise action says “no.”

Question 2

What does “repeat 4 times: turn left” tell the computer to do?
  1. Turn left four times
  2. Turn left once, then stop
  3. Turn left five times
  4. Check whether turning left is true
Show answer and explanation
Turn left four times
The repeat count tells how many times the instruction inside runs: four.

Question 3

A program must check each of five inputs and choose a message for each one. Which plan fits?
  1. Use one conditional and skip the other inputs.
  2. Repeat the check five times, using a conditional for each input.
  3. Repeat the message five times without checking inputs.
  4. Use a repeat count of one.
Show answer and explanation
Repeat the check five times, using a conditional for each input.
The repeat handles all five inputs, and the conditional chooses the correct message for each one.

Key terms

Code
Instructions written for a computer to follow.
Condition
A question or rule that has a true or false answer.
Conditional
A code structure that chooses an action based on a condition.
Control structure
A way to organize code, such as choosing an action or repeating instructions.
Repeat structure
A control structure that runs an instruction a set number of times.
Input
Information given to a program.
Trace
Follow code in order and record what happens.
Efficient code
Code that does the needed job clearly without unnecessary repeated instructions.

Continue through Grade 6 Math

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

About this lesson and its review

Published by DoAssignment. This reviewed lesson follows Ontario Grade 6 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