Learning how to use loops to repeat actions without writing the same instruction many times.
Imagine your teacher asked you to write the word 'Hello' 100 times on the board. Your hand would get so tired! What if you could just give a robot one single command to do all that work for you in a blink?
In the world of robots and computers, a loop is a way to tell a machine to do the same thing over and over again. Instead of writing the same instruction many times, we use a loop to save time. Think of a loop like a hula-hoop or a wheel; it starts at the beginning, goes all the way around, and then starts again! If you want a robot to take 10 steps, you don't have to say 'Step' 10 times. You just say: Repeat 10 times: Step.
Quick Check
What is the name of the tool programmers use to make an action happen more than once?
Answer
A loop
Loops make your code efficient. Being efficient means doing a job well without wasting time or energy. Imagine writing a program to make a robot dance. If the dance has 50 claps, writing 'Clap' 50 times would take a long time and you might lose count! With a loop, you only write the word 'Clap' once. This makes your code shorter, cleaner, and much easier for other people to read.
Let's look at two ways to make a robot jump 3 times.
The Long Way: 1. Jump 2. Jump 3. Jump
The Loop Way: 1. Repeat 3 Times: - Jump
Both do the exact same thing, but the loop is much faster to write if we wanted to jump or times!
Quick Check
If you want a robot to blink 5 times, which is more efficient: writing 'Blink' 5 times or using a loop?
Answer
Using a loop
To build a loop, you need two pieces of information: What to do and How many times to do it. We call the 'how many times' part the count. In math terms, if you repeat an action times, the robot performs the task exactly times. For example, if , the robot does the action 3 times. This is much simpler than writing a list that is items long!
To draw a square, a robot needs to move forward and turn 4 times.
1. Repeat 4 Times: - Move Forward 5 inches - Turn Right 90 degrees
By using a loop with a count of , we create a perfect square with very little code!
A robot needs to plant 3 flowers. For each flower, it must dig a hole and drop a seed.
1. Repeat 3 Times: - Dig a hole - Drop 1 seed - Move forward 2 feet
This loop repeats a group of three actions. Since the loop runs 3 times, the robot will dig a total of holes and drop a total of seeds.
What does a loop do in a computer program?
If a loop says 'Repeat 5 Times: Clap', how many times will the robot clap?
Using a loop is usually more efficient than writing the same instruction over and over.
Review Tomorrow
Tomorrow morning, try to remember the two things every loop needs (the 'what' and the 'how many').
Practice Activity
Look for 'loops' in your real life! For example, when you brush your teeth, do you repeat the same scrubbing motion? How many times do you think you 'loop' that action?