Skip to content

Sample lesson · grade 6

Algorithms and loops

45 minmaterialexercisescodequizAI Tutor

See what a complete CodeSmarter lesson looks like, from introduction to knowledge check.

01Introduction

What is a loop and why do we use it?

A loop is an instruction that repeats the same part of a program many times. Instead of writing five identical lines, you describe one action and tell the computer how many times to run it.

On every repetition the program checks a condition: if it is true, it runs the instructions inside the loop and goes back to the start. When the condition is no longer true, the loop ends.

StartIs the counter still in range?
yes
Run the instructions and increase the counter
no
End
02Example

One loop, five lines of output

for i in range(1, 6):
    print(i)

Output

12345
range(1, 6)
The range starts at 1 and stops before 6. The last number is not included.
i
The loop counter. Each repetition gives it a new value: 1, 2, 3, 4, 5.
print(i)
The instruction inside the loop. It runs once per repetition.
03Exercise

Your turn

Code editor · Python10 pkt

Change the code so the program prints the numbers from 1 to 10.

Console

Run the code to see the output.

04AI Tutor

The student is never left alone with a mistake

Alex

Alex

Look at the second argument of range(). What value makes the loop reach 10?

Need a hint?

The hint guides towards the answer instead of giving it away.

See the full AI Tutor demo
05Knowledge check

One question to finish

How many times does for i in range(1, 5) run?

06Lesson summary

After this lesson the student can:

  • explain what a loop is
  • recognise when a loop is the right tool
  • write a simple for loop
  • fix a basic mistake in a loop

That is one lesson. Now see all of CodeSmarter.

In the demo we walk through the grade 4–8 curriculum, the teacher panel and school rollout.