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 1 point...

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 2 points...

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 3 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.

If you get 4 points...

To manage situations more effectively, it's essential to use nested conditionals. This involves placing one conditional statement inside another, which allows for more nuanced and sophisticated decision-making in your code.

                        if << (score) > (50) > and <touching [Power-Up v]? >> then
                            if <(lives) > [1]> then
                                change [score v] by (10)
                            else
                                change [lives v] by (1)
                            end
                        else
                            if <(time) < (30)> then
                                say [Hurry up!] for (2) secs
                            else
                                stop [all v]
                            end
                        end
                    

How this block works? The outer 'if' statement checks if both conditions: 'score greater than 50' AND 'touching Power-Up' are true. If both conditions are true, it evaluates the nested 'if' statement inside. The nested 'if' checks if the number of lives is greater than 1. If this condition is true, the score is increased by 10. If not, the number of lives is increased by 1.

If the outer 'if' condition is false, it evaluates another nested 'if' statement that checks if the time is less than 30. If this condition is true, the sprite says 'Hurry up!' for 2 seconds. If not, the script stops all.

Using nested conditionals allows you to create more nuanced and detailed responses in your projects, making them more interactive and responsive to multiple conditions at once.