Flow Control

Instructions related to algorithmic notions of flow control can help you to control the behavior of your characters, making, for example, certain blocks that are repeated a number of times, or until a situation arises.

If you get 1 point...

The most basic way to control the behavior of your characters is creating a program composed of a set of blocks are executed one after another, as we see in the picture:

                    when green flag clicked
                    say [I'm a dancer cat] for (2) secs
                    move (10) steps
                    turn cw (15) degrees
                    wait (0.5) secs
                    turn ccw (15) degrees
                    move (-10) steps
                

How does this program works? When the user click on the green flag run behind one another all blocks that have been included in the program. Start with the first block 'say I'm a cat dancing for 2 seconds', then run 'move 10 steps' then 'turn 15 degrees right', and so on to the last block of the program.

If you get 2 points...

Sometimes when we want a set of blocks is constantly repeated, instead of repeating the same blocks over and over again, you can use a repeat instruction allowing achieve the same effect, but more comfortable and manageable way. Consider a couple of examples:

                    when green flag clicked
                    say [I'm a dancer cat] for (2) secs
                    repeat (3)
                    move (10) steps
                    turn cw (15) degrees
                    wait (0.5) secs
                    turn ccw (15) degrees
                    move (-10) steps
                    wait (0.5) secs
                    end
                    when green flag clicked
                    forever
                    move (10) steps
                    if on edge, bounce
                

How these control blocks works?
In the block 'repeat' when execution reaches this point is repeated as often as has been indicated in the parameter block 'repeat', in the example would be 3 times, all blocks included within this block. We can see that the block 'repeat' has an arrow at the bottom indicating that when they finish executing the blocks back again up.
The block 'forever' works like the 'repeat' but never finish executing the blocks containing in its interior.

If you get 3 points...

Sometimes we don't know previously the number of times you want a set of blocks is executed, as this depends on a given situation. In these cases, the block 'repeat until ...' is really useful. Here's an example:

                    when green flag clicked
                    repeat until < touching [Enemy v] ?>
                    say [You don't catch me]
                    end
                    say [You catch me]
                

In the example, the character will constantly repeat the instruction say 'You don't caught me...' while he don't touch the enemy. At the time that the condition is met instruction 'repeat until ...', the contents inside will finish run and will jump to the next instruction, in this case say 'You caught me!'.

If you get 4 points...

To achieve a score of 4 points, you need to use Nested Loops within your project. This involves placing one loop inside another to perform more complex repetitive tasks. Here's an example of nested loops:

                    when green flag clicked
                    repeat (5)
                    repeat (3)
                    move (10) steps
                    turn cw (15) degrees
                    wait (0.5) secs
                    end
                    say [Finished a round!]
                    wait (1) secs
                    end
                

In this example, the character will repeat the inner loop 3 times, where it moves, turns, and waits. This entire set of actions will be repeated 5 times by the outer loop. After completing the repetitions, the character will display a message and wait before restarting the process.