Logic in Scratch

What is Logic?

This question may seem very broad, but has some specific applications within the world of programming, and as an extension, Scratch. The understanding and implementation of logical functions is one of the key factors that makes programs able to think. Basically, logic is what allows the program to evaluate certain parameters and then make decisions based on the evaluations. Let’s examine three categories: Conditionals, Comparison Operators, and Booleans. Each of these categories are interconnected.

Understanding and practicing logic in Scratch is crucial to building more complex and interactive projects. The interplay of these logical categories drives much of the action in games/apps and are present in nearly every game mechanic. As this is the case, the logic blocks (they span several block categories) are important for success in Scratch. Scratch is also a fantastic platform for building a solid logical foundation for youth. This is one of the best examples of Scratch’s language agnostic preparation for future languages as youth are able to develop these foundations that can then transfer over to any future programming language and syntax.

Booleans

Booleans blocks are the hexagonal blocks that contain a condition. Boolean expressions are in nearly every programming language as they are one of the core building blocks of code. Boolean expressions are expressions that produce a Boolean value (such as true or false) once evaluated.

In Scratch, Booleans are the hexagonal blocks that report either true or false once the block is invoked in the script. Within Scratch, there are 13 Boolean blocks that can all be found in various categories such as Sensing, Operators, and Variables. You’ll work more with each of these categories throughout the workshop, but for now the most important part to understand is that Booleans evaluate to either true or false. This fact is interconnected with the next type of logic, conditional statements.

Conditional Logic

Without the use of conditional logic, your program will execute all commands that are given to it as long as they are connected to an event block. Conditional logic is what causes the program to briefly pause and evaluate if something is true before deciding to execute code. Here’s what this looks like in pseudo code:

This breaks down in the following way:
The first line evaluates whether or not the (condition) is true. If it is true, then the actions in the brackets are executed. If not, the program won’t execute the code in the brackets. Scratch’s conditional statements work exactly the same way, but are represented visually:

IntGames - Logic in Scratch - Blocks 1

The structure of this is the same as the above code with one brief change: the addition of the forever loop. The if/then block is contained within the forever loop because Scratch needs to know to always be looking for the condition and evaluating it. Without this loop, Scratch will only look for it a single time and then stop, which doesn’t quite work. This is a quirk that is particular to Scratch. Just remember that when in nearly every case where you’re using conditional statements in Scratch that you need to contain the script within a forever loop.

Script Analysis

In the script above, Scratch checks to see if the space key is pressed, and then assigns a true or false value to that. The “key[space] pressed?” block is a Boolean block and the value can either be true or false.

If the value is true, Scratch then executes the code contained within the if/then block. This changes the y value of the sprite by 10. Don’t worry too much about what this code does in itself. Instead, make sure to understand the structure and function of conditional statements within Scratch.

The conditional logic blocks are contained in the Control block category and look like the letter C, and so are occasionally referred to as a “C block.”

You can also use the “if / then / else” block. This allows for an additional input where you can add more scripts that will execute based on whether the condition met is true or false.

Operators

There are two types of operators that you’ll be reading about in this section. The first type of operators are comparison operators. These test the relationship between two things. Comparison operators are also referred to as “relational operators” in programming. For example, these are used to test for equivalence in the case of 5 = 5, or to test inequalities such as 4 > 3. In these examples, the = and > signs are the operators.

Scratch supports the following comparison operators:

  • [ ] < [ ]
  • [ ] = [ ]
  • [ ]> [ ]

The second type of operators are logical operators. These determine the logical relationship between two things. For example, in the statements “x AND y” and “x OR y,” the logical operators are the AND and the OR.

Scratch supports the following logical operators:

  • [ ] AND [ ]
  • [ ] OR [ ]
  • NOT [ ]

Logical operators are often paired with variables, which are covered in a later article.

Operators in Scratch

In Scratch, operators have their own block category. All the operator blocks are light-green in color. There is overlap within these operators and the Boolean operators, as many of the operator blocks report Boolean values. Don’t worry so much about this right now- just focus on understanding when to use each block.

IntGames - Operators in Scratch

Notice that both the logical and comparison operators in the above image are hexagonal. This means that these are Boolean blocks as well. These blocks evaluate to either true or false and are often used in conditional statements.

Here is an example combining all of the above logic:

IntGames - Logic Example

Script Analysis

This is how the script breaks down:

  1. First is the gameStart Hat block, telling Scratch to execute this script once the Start flag is clicked.
  2. Next we have the forever loop, telling Scratch to always check for and evaluate the condition.
  3. The conditional statement contains a logical operator that is comparing a random number to a set value. If the returned value is true (=3), then the “change [color] by [50]” block executes.
  4. Notice that this conditional statement contains an else. This means tells Scratch that if the operator returns any other number besides 3 that Scratch will then execute the “change [color] by [25]” code.

Related Posts

  • rainbow squiggly scratch art made with a CD

    This is a fun and creative project and something great to do with old CDs…

  • the Scratch logo with the Scratch cat mascot

    What is Scratch? Scratch is a free web-based visual game engine developed by the MIT…

  • the Scratch logo with the Scratch cat mascot

    Managing Code One important soft-skill that is worth mentioning is understanding the importance of managing…