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
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
- 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
- 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
- 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
Hypothetical input-output examples for the stated rule. The vertical numbers stand for the two messages: 0 means “try again” and 1 means “ready.”
- State the conditionThe program needs to test whether score is at least 6. “At least” includes 6 itself, so use a condition that includes equality.
- Write both choicesIf the condition is true, the program says “ready.” Otherwise, it says “try again.” The otherwise action covers inputs that are less than 6.
- Trace the inputsFor input 4, the condition is false, so the message is “try again.” For input 8, it is true, so the message is “ready.”
Worked example
Repeat a movement instruction
- Find the repeated actionThe action that happens again is “move forward.” Put that instruction inside a repeat structure.
- Set the repeat countThe character must move exactly 5 spaces, so the instruction runs 5 times. The repeat count is the number of times the instruction runs.
- Check the resultCount one movement each time through the repeat structure. Five repetitions produce five movements, not six.
Worked example
Use a condition inside a repeat
Hypothetical inputs for the repeated conditional. The vertical numbers stand for messages: 1 means “match” and 0 means “not a match.”
- Place the check inside the repeatThe program must handle three inputs, so use a repeat structure for three turns. During each turn, check the current input with a conditional.
- Write the rule clearlyFor each input, the condition is “current input equals 2.” If true, say “match”; otherwise, say “not a match.”
- Trace each turnThe 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
Common mistakes and how to avoid them
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
- yes
- no
- It repeats the check four times.
- There is not enough information.
Show answer and explanation
Question 2
- Turn left four times
- Turn left once, then stop
- Turn left five times
- Check whether turning left is true
Show answer and explanation
Question 3
- Use one conditional and skip the other inputs.
- Repeat the check five times, using a conditional for each input.
- Repeat the message five times without checking inputs.
- Use a repeat count of one.
Show answer and explanation
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
- C2.4 · Solve and graph two-operation whole-number inequalities
- C3.2 · Change code and explain effects on output and efficiency
- 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.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.