Computer Science
Grade 5
20 min
Giving Clear Directions
Giving Clear Directions
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Deconstruct a complex task into a sequence of simple, unambiguous steps.
Write directions that use variables as parameters to make them reusable and flexible.
Use complex conditionals (if/else if/else) to create directions that adapt to different situations.
Use loops with variables to write efficient directions for repetitive tasks.
Define and call a procedure (a named set of directions) to organize their code.
Identify and correct ambiguity in a set of instructions to prevent errors.
Have you ever given directions to a friend who got totally lost? 🤖 Computers are even worse—they do *exactly* what you say, even if it's silly!
In this lesson, we'll learn advanced ways to give directions to a computer. We'll make our instructions super cle...
2
Key Concepts & Vocabulary
TermDefinitionExample
Procedure (or Function)A named set of directions that can be used over and over again just by calling its name.A procedure named `drawSquare` could contain all the steps for moving and turning to draw a square. Then you just have to write `drawSquare()` instead of repeating all four `move` and `turn` commands.
ParameterA special variable that acts like a fill-in-the-blank for a procedure, allowing you to give it specific information each time you call it.In `drawSquare(size)`, the `size` is a parameter. You can call `drawSquare(50)` for a small square or `drawSquare(200)` for a big one, using the same set of directions.
Complex ConditionalA way to make decisions with more than two choices, using `if`, `else if`, and `else` to check multiple conditions.`IF enemy is ne...
3
Core Syntax & Patterns
The Reusable Procedure Pattern
PROCEDURE procedureName(parameter1, parameter2) { ... a block of code ... }
Use this to group a set of commands you'll use multiple times. Define it once, then 'call' it by name whenever you need it, filling in the parameters.
The Multi-Path Conditional Pattern
IF (condition1) { ... do this ... } ELSE IF (condition2) { ... do that ... } ELSE { ... do this other thing ... }
Use this when your program needs to choose between several possible actions based on different conditions. It checks each condition in order until one is true.
The Counting Loop Pattern
FOR counter = start TO end { ... do something using 'counter' ... }
Use this when you need to repeat an action a specific number of times and need to keep trac...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
A robot needs to draw 4 lines. The first line should be length 10, the second length 20, the third length 30, and the fourth length 40. Which set of directions correctly uses a variable-driven loop?
A.FOR i = 1 TO 4 { drawLine(10) }
B.drawLine(10) drawLine(20) drawLine(30) drawLine(40)
C.FOR i = 1 TO 4 { drawLine(i * 10) }
D.FOR i = 10 TO 40 { drawLine(i) }
Challenging
A friend writes code to draw a red triangle and then a blue pentagon. They write out all the `move` and `turn` commands for the triangle, then all the commands for the pentagon. Using the `drawPolygon(numberOfSides, length)` procedure, how could you BEST improve their code to also let them change the color?
A.Create a new procedure `drawRedTriangle()` and another `drawBluePentagon()`.
B.Use `drawPolygon(3, 50)` and `drawPolygon(5, 50)` but tell them to change the pen color manually.
C.Create a new, more powerful procedure: `drawColoredPolygon(sides, len, color)`.
D.It can't be improved; their way is the clearest.
Challenging
The 'Smart Robot Collector' needs an update. There are now regular green gems (20 points) and special 'shiny' green gems (100 points). The robot can check `gemColor` and `isShiny`. What is the correct conditional order to score points properly?
A.IF (gemColor == "green") { score + 20 } ELSE IF (isShiny) { score + 100 }
B.IF (gemColor == "green" AND isShiny) { score + 100 } ELSE IF (gemColor == "green") { score + 20 }
C.IF (gemColor == "green") { score + 20 } ELSE IF (gemColor == "green" AND isShiny) { score + 100 }
D.IF (isShiny) { score + 100 } ELSE IF (gemColor == "green") { score + 20 }
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free