Block programming: sequences and loops
Machine translation
Learn
A program is an algorithm written for a computer or a robot to follow. In block programming, each block is an instruction.
move forwardmove forwardturn rightmove forwardA program made of four blocks The blocks go one below another. The robot follows them from top to bottom, one after another. That is a sequence. If you change the order, the robot may end up somewhere else.
The robot is facing one way. “Move forward” moves it 1 square in the direction it is facing. “Turn right” only changes the direction it is facing. It does not move it to another square.
It starts facing up: move forward 2, turn right and move forward 1 If you have to do the same thing many times, do not write lots of identical blocks. Use a loop. A loop repeats blocks. “Repeat 3 times” does what is inside 3 times. What is inside goes a little further to the right. This is called indentation.
Without a loop:move forwardmove forwardmove forwardmove forwardWith a loop:repeat 4 timesmove forwardBoth programs do the same thing Loops are also useful for counting. “Repeat 3 times: put down a basket with 2 bread rolls” puts down 3 baskets. In total there are 6 bread rolls.
3 baskets with 2 bread rolls in each
Worked examples
Example 1
Gus programs a robot. It starts at the bottom left, facing up. Its program is: move forward, turn right, move forward, move forward. Where does it end up?
- Move forward: go up 1 square.
- Turn right: it now faces right, but it stays in the same square.
- Move forward twice: it moves 2 squares to the right.
It ends up 1 square up and 2 to the right of the start.
Watch out! Many people here think that turning right moves the robot 1 square.
Example 2
Julie writes this program: repeat 3 times, move forward and turn left. How many times does the robot move forward? How many times does it turn?
- What is inside the loop is move forward and turn left.
- The loop repeats it 3 times.
- It moves forward 3 times and turns 3 times.
It moves forward 3 times and turns 3 times.
Watch out! Many people here say it moves forward 1 time, because they see only 1 “move forward” block.
Example 3
Tony writes four “move forward” blocks in a row. How can he write it with a loop?
- Look at which block repeats: move forward.
- Count how many times it repeats: 4.
- Write “repeat 4 times” and put “move forward” inside it, with indentation.
Repeat 4 times, and inside: move forward.
Watch out! Here lots of people write “repeat 4 times” and leave “move forward” outside, without indentation.
A challenge to think about
On a piece of paper, write a program with blocks so that a friend walks once around a chair. Use a loop so you don't repeat the blocks. Your friend acts as the robot and only does what the paper says. Do they get there?
Practise
Rounds of 10 questions. Choose a level:
Your grade in this topic:No grade yetDo a round at the Basic level to get a grade.
Next topic: Conditionals and variables in block-based programming →