Definition: A loop is a set of instructions that are repeated. Also known as iteration.
By the end of this lesson, you will be able to:
Imagine you're teaching a robot to do jumping jacks. Would you want to tell it "jump, jump, jump..." 50 times? Of course not! That's where loops come in - they're like a magic spell that makes the computer repeat instructions for you! :emoji:
:information_source: Remember! Loops are one of the most powerful tools in programming. They turn repetitive tasks into simple, elegant code!
To access loop blocks in EduBlocks:
Think of a While loop like a bouncer at a club - it keeps checking a condition and only lets the code inside run if the condition is True! :emoji:
A While loop keeps repeating as long as its condition remains True. It's perfect for:
:bulb: Pro Tip!
Be careful with While loops! If the condition never becomes False, your loop will run forever (called an "infinite loop"). Always make sure there's a way for the loop to end!
Just like the if, elif or else blocks, a While loop will only run the commands inside it if the condition is True.
Steps to create a While loop:
Input | Output |
---|---|
![]() |
![]() |
In text-based Python, a While loop looks like this:
while condition:
# Code to repeat goes here
The blocks make it visual, but it's the same concept!
While loops are great when you don't know how many times to repeat, but what if you want to repeat exactly 10 times? That's where For loops shine! ⭐
For loops are perfect when you:
Let's create a For loop that repeats 5 times:
Steps to create a For loop:
i
for "index")Input | Output |
---|---|
![]() |
![]() |
:information_source: Remember! In a For loop with range 5:
i
starts at 0 (not 1!)i
increases: 0, 1, 2, 3, 4The i
variable is special - it's the current index number of the loop:
:bulb: Pro Tip!
You don't have to use the
i
variable in your code! It's there to help the computer count, but you can ignore it if you just want to repeat something.
Input | Output |
---|---|
![]() |
![]() |
In this example, the i
variable isn't used inside the loop, but it's still needed for the computer to track how many times it has repeated.
In text-based Python, a For loop looks like this:
for i in range(5):
# Code to repeat goes here
One of the coolest things about For loops is that they can go through each item in a list automatically! It's like having a robot assistant that checks every item in your backpack one by one. :emoji:
A For loop can loop through items in a list, processing each one in order.
Understanding the parts:
x
represents the current item being processedy
should be a list variable containing your itemsSteps to loop through a list:
x
with a meaningful variable name (like "item" or "fruit")y
with your list variable nameInput | Output |
---|---|
![]() |
![]() |
:information_source: Remember! The loop goes through the list in order - first item first, last item last. The variable
x
(or whatever you name it) holds the current item being processed.
:bulb: Pro Tip!
Choose meaningful variable names! Instead of
x
, use names like:
fruit
for a list of fruitsnumber
for a list of numbersplayer
for a list of player names
Sometimes you need an emergency exit from a loop - that's where the break block comes in! It's like having an escape button when you've found what you're looking for. :emoji:
The break block immediately stops a loop and exits to the code after the loop.
Break is useful when:
Steps to use the break block:
Input | Output |
---|---|
![]() |
![]() |
:information_source: Remember! You can place the break block anywhere inside a loop - in While loops, For loops, or even nested loops! When break runs, it only exits the innermost loop it's in.
:bulb: Troubleshooting Tip!
If your loop isn't breaking when expected:
- Check that your condition is correct
- Make sure the break is inside the conditional block
- Add print statements to see what's happening
- Remember that break only exits one loop level
In text-based Python, break works exactly the same way:
for item in my_list:
if item == "stop":
break
print(item)
Ready to level up your loop skills? Try these challenges:
Create a While loop that keeps asking for a password until the user enters the correct one. Use break when they get it right!
Make a For loop that counts from 1 to 100, but skips all numbers divisible by 7.
Create a list of your favorite games. Use a For loop to print each one, but stop (break) if you find "Minecraft" in the list.
Use nested loops (a loop inside another loop) to create interesting patterns with print statements or Turtle graphics!
Congratulations! You've mastered loops - one of the most powerful concepts in programming! You can now:
Loops are everywhere in programming - from games that run until you quit, to apps that process your photos one by one. You're now ready to create more complex and interesting programs!
Keep practicing with loops - they're the key to making your programs do amazing things without writing tons of repetitive code! :rocket: