Computer Science Grade 5 20 min

Loops - Repeat Actions

Loops - Repeat Actions

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Define a nested loop and explain how it works. Use a variable to control the number of times a loop repeats. Trace the value of a loop control variable through each iteration. Create a loop that counts down instead of up. Design a simple pattern, like a square or a triangle, using nested loops. Combine a loop with a conditional (if-statement) to perform actions on specific iterations. Ever wonder how a game can draw a whole grid of blocks or a sky full of stars in an instant? 👾 It's not magic, it's super-powered loops! Today, we're going beyond simple 'repeat 10 times' loops. You'll learn how to use variables to make your loops smarter, more flexible, and powerful enough to create amazing patterns and solve complex problems...
2

Key Concepts & Vocabulary

TermDefinitionExample Loop Control VariableA variable that keeps track of how many times a loop has run. It usually starts at a number, and changes with each loop cycle.In `for i from 1 to 5`, the variable `i` is the loop control variable. It will be 1, then 2, then 3, then 4, then 5. IterationA single time the code inside a loop is executed.If a loop runs 4 times, it has 4 iterations. The first time through is the first iteration. Nested LoopA loop that is placed inside of another loop. The inner loop will run completely for each single iteration of the outer loop.To draw a 3x3 grid, you can have an outer 'row' loop that runs 3 times, and an inner 'column' loop that also runs 3 times. IncrementTo increase the value of a variable, usually by one.If our loop variable `c...
3

Core Syntax & Patterns

The 'For' Loop Pattern FOR variable FROM start_value TO end_value { ... code to repeat ... } Use this when you know exactly how many times you want to repeat an action. The 'variable' will automatically count from the start value to the end value. The Nested Loop Pattern FOR row FROM 1 TO 5 { FOR column FROM 1 TO 5 { ... draw a dot ... } } Use this to work with 2D shapes like grids, tables, or game boards. The outer loop handles the rows (up/down), and the inner loop handles the columns (left/right) for each row. Using the Loop Variable Inside FOR i FROM 1 TO 4 { print 'Lap number ' + i } The loop control variable isn't just for counting! You can use its value inside the loop to change what happens in each iteration. This is key for...

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
Which code snippet would produce a 5x5 HOLLOW square of stars (*), where stars are only on the border?
A.FOR r FROM 1 TO 5 { FOR c FROM 1 TO 5 { IF r==c { print '*' } ELSE { print ' ' } } }
B.FOR r FROM 1 TO 5 { FOR c FROM 1 TO 5 { print '*' } }
C.FOR r FROM 1 TO 5 { FOR c FROM 1 TO 5 { IF r==1 OR r==5 OR c==1 OR c==5 { print '*' } ELSE { print ' ' } } }
D.FOR r FROM 1 TO 5 { FOR c FROM 1 TO r { print '*' } }
Challenging
What is the final value of the `result` variable after this code runs? `result = 10; FOR outer FROM 1 TO 2 { FOR inner FROM 5 DOWNTO 4 { result = result - inner } }`
A.1
B.-8
C.2
D.6
Challenging
Analyze this code. What pattern will it draw? `FOR row FROM 1 TO 5 { FOR col FROM 1 TO 6 - row { print '*' } print_new_line() }`
A.5x5 square.
B.staircase that gets wider at the bottom.
C.pyramid.
D.right-angled triangle that is widest at the top (an upside-down staircase).

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Advanced Topics

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.