Beginner Coding with Blocks
Chapter 4
>
Level 4

Loops and Repetition
For Loop Sequence

Objective

Collect grains and make your way out of the maze by using no more than four (4) blocks.

You've found yourself lost in the middle of a maze, use loops in order to collect the grains in the field and exit the maze.

As you can see from the map the maze is uneven so you can't just repeat the same movement like in previous levels. For this you need to make full use of the for loop block's variable.

Use the for loop block with a variable named x which checks how many cycles the for loop block has gone through. Set it to start from 0 and go to a number (like 11). The by 1 at the end says how many units it goes though, in the following example the loop starts at 0, in increments 1 number at a time up until it reaches 11.

Block Diagram

We’ll be using the loop to increment the number of steps you take each cycle in order to reach the end. To do this we use the operator block with the move forward block to in order to move one more step each time you loop. To do this, we drag & drop the operator block from the Math menu, place it inside a move forward block, and add the X standalone variable from the variables menu.

Block Diagram

We set the operator to sum + and the number to 1 so each loop cycle the number of steps the player takes is longer. In practice It looks like this:

  • First cycle x is 0 (x + 1 = 1) player takes 1 step
  • Second cycle x is 1 (x + 1 = 2) player takes 2 steps
  • Third cycle x is 2 (x + 1 = 3) player takes 3 steps

And so forth-

Since the maze requires you to change direction, use the turn left block at the end of the loop so each cycle you change direction after moving. With this you will be able to collect all the grain in the map and complete the level.

Block Diagram

Remember, this must be done by using no more than four (4) blocks.

Code book