Logic

Instructions related to logical thinking can help your projects are dynamic, so that they behave differently depending on the situation. In the stories, for example, these instructions are not as important as they usually have a linear structure we always want to run the same way, but in other projects, such as video games are essential to perform different actions depending on the situation.

If you get 0 points...

The most basic block you can start working logical thinking is this:

                        if < touching color [#0000FF]?  >then
                        say [I have reached the goal!] for (2) secs
                    

How this block works? When execution reaches this point, the condition on the block is evaluated, and if this is true, the set of blocks that are within the if executed. In the example, if the character is touching the blue color, he says 'I reached the goal.'

If you get 1 point...

Now that you know the instructions if, perhaps you can start using blocks if / else, which can be very practical in many types of projects. The blocks if / else are very similar to the block if, but contain two sets of blocks inside.

                        if < touching color [#0000FF]? >then
                        say [I have reached the goal!] for (2) secs
                        else
                        say [I have reached the goal!] for (2) secs
                    

How this block works? When execution reaches this point, the condition on the block is evaluated, and if this is true, the set of blocks that are within the 'if' is executed, but if the condition is false, then the set of blocks that are within the 'else' is executed. In the example, if the character is touching the blue color, he says 'I reached the goal!', but ,if you are not touching the purple color, says 'I have not reached the goal'.

If you get 2 points...

To make decisions on some projects sometimes is necessary to evaluate more than one condition at the same time to know what to execute. In these situations it is very helpful to use logical operations that combine the conditions. Logical operations are available in Scratch AND, OR and No.

                        if << touching [Enemy v]? > and <(lives) = [0]>> then
                        say [Game over]
                        stop [all v]
                        end
                        if << touching [edge v]? > or < touching [Enemy v]? >>then
                        change [lives v] by (-1)
                        end
                        if < not <(level)=(5)>>then
                        say [You still have undiscovered levels] for (2) secs
                        end
                    

The operation AND is true when both conditions are true evaluated. The OR operation is true when one of two conditions are true evaluated. And the operation otherwise not worth the condition, ie, if the condition is true, not returns false and vice versa.