Repeating and choosing: loops and conditions
With a loop you make a computer repeat something, with a condition you let it choose. That is how long programs become short and clever.
Why
You want a robot to move ten squares forward. Do you really have to write “move forward” ten times?
You could, but it is boring and you soon make a mistake. Programmers prefer to write it once: repeat 10 times, move forward.
And what if there is a wall? Then the robot has to be able to choose: if there is a wall in front of you, turn, otherwise move forward. With repeating and choosing, a short program can do a great deal.
See it
Choose a program and play it step by step. On the left are the blocks in your own language, on the right the same lines as code. The line that is running lights up.
- repeat 4 times
- move 1 square forward
- move 1 square forward
- turn right
for i in range(4):move()move()turn_right()
The blocks and the code are the same program, line for line. The code looks like Python and uses English words, like real code.
Description of the picture
A grid with columns A to F and rows 1 to 6, a robot with a nose that shows where it is looking, sometimes walls and a flag. Next to the grid the program appears twice: as blocks, such as repeat 4 times and turn right, and as code, such as for i in range(4) and turn_right(). At every step the robot moves and the line it is carrying out lights up.
Get it
A program is a list of instructions that the computer carries out one by one, from top to bottom. That is called a sequence.
A loop repeats the instructions inside it. In the blocks it says repeat 4 times, in the code for i in range(4):. What belongs inside is moved a little to the right. range(4) counts 0, 1, 2, 3: those are four rounds.
A condition lets the computer choose. if wall_ahead(): means: if there is a wall in front of you. Then it does what is underneath, and otherwise (else:) something else.
A loop with a condition inside is very powerful. while path_ahead(): means: as long as the way is clear, keep repeating. The robot stops by itself at the wall, however far away it is.
In Lus you always see two forms of the same program: blocks in your own language and code with English words, as real programming languages use them. They are the same lines, with the same number.
Sequence: from top to bottom. Loop: repeat what is indented. Condition: if this is true, do that, otherwise something else.
Worked example
The robot is on A5 and is looking up. Where is it after this program?
The program:
for i in range(3): move() turn_right() move() move()The loop runs 3 times: the robot goes from A5 to A4, A3 and A2.
Then it turns right and moves two squares forward: B2, C2.
- Answer
The robot is on C2 and is looking to the right.
Do it
Choose a level. You get a new exercise every time, checked straight away.
Pitfalls
Wrong:
range(4)counts 1, 2, 3, 4.Python starts counting at 0. There are four rounds, but the counter is 0, 1, 2 and 3.
Right:
range(n)gives n rounds, with the counter going from 0 to n − 1.Wrong: Everything below a loop gets repeated.
Only the lines that are indented belong to the loop. A line that starts at the left again only comes after the loop.
Right: Look at the indentation: it shows what belongs to the loop or to the condition.
Wrong: A
whileloop stops after a fixed number of times.A
whileloop repeats as long as the condition is true. If it never becomes false, the loop never stops.Right: Make sure something changes in the loop, so that the condition becomes false at some point.
In real life
- A traffic light repeats its loop all day long: green, amber, red, and back to the start.
- A robot vacuum cleaner drives straight on as long as the way is clear, and turns when it feels a wall or a chair leg.
- In a game the computer checks again and again: if you touch the coin, you get a point.
- A thermostat keeps checking: if it is colder than the setting, the heating goes on.
History
In 1843 Ada Lovelace wrote notes on the Analytical Engine of Charles Babbage, a calculating machine that was never fully built. In them she described step by step how the machine could calculate a series of numbers, with steps that are repeated. That is why she is often called the first programmer.
At the table
Play robot together on the kitchen tiles. One person writes a program with repeat and if, the other carries it out literally. Can you get to the fridge?
A question to talk about together, for parents and children. There is no wrong answer.