(Redirected from Boolean)

Wikipedia-logo.svg  For more information, see Boolean data type on Wikipedia.

The general shape of a Boolean block

A Boolean block is an elongated hexagonal block that reports boolean values (values with only true or false). When the block is used, it acts as a reporter block, reporting "true" or "false" string values or the numbers "1" and "0" depending on its usage in a script.

There are 20 Boolean blocks, and they can be found in the Sensing, Operators and Variables categories. Custom blocks can have Boolean inputs that may be present in a block definition.

Blocks

There are 20 Boolean blocks in Scratch 3.0.

Note Note: Click on a block for more information.

Sensing

Operators

List

micro:bit

LEGO MINDSTORMS EV3

LEGO BOOST

LEGO Education WeDo 2.0

Go Direct Force and Acceleration

Removed

Archive.png This article or section documents something not included in the current version of Scratch (3.0). It is only useful from a historical perspective.

Shape

Boolean blocks are conditions that can either be true or false. They have a hexagonal shape and fit in the corresponding hexagonal slot on other blocks.

The condition gap can be filled with any Boolean block:

if <key (space v) pressed?> then
    broadcast (Jump! v)
end

Despite their shapes, Boolean blocks also fit into string and number inputs:

when gf clicked
forever
    say <touching (Sprite1 v)?>
end

In general, when inserted into a reporter input, Scratch will automatically convert the boolean values of false and true into 0 and 1 respectively. This allows for mathematical operations to be performed by inserting booleans into reporters. For example, consider the script below:

when gf clicked
forever
  say ((3) + <touching (Sprite1 v)?>)
end

If the boolean condition is true, the sprite will say 4 as the boolean value of true is automatically converted to 1, and 3 + 1 = 4. Conversely, if the Boolean condition is false, the sprite will say 3 as the false boolean value is converted to a 0.

Note that this conversion only occurs when a boolean input is inserted into a number input. If the () + () block is omitted entirely, the boolean will then return true or false because the boolean block is in a string input (which doesn't convert booleans to numbers).

Uses

As Boolean blocks are conditions (and report if they are true or false), they are used whenever a condition is needed. Conditions are used with some C blocks and some Control Stack blocks. A common use for conditions is the If () Then block — if the condition is true, the blocks held inside the C block will activate.

There are a variety of different conditions that can be checked, from checking if the mouse is touching a sprite to checking if a value is equal to another value. An example is below:

when flag clicked
wait until <touching (edge v)?>
say [Done!] for (2) seconds
stop [this script v]

The Wait Until () block pauses the script until the Boolean value, here <touching (edge v)?>, is true. While the sprite is not touching the edge, <touching (edge v)?> is false, and the block waits for it to become true. When the sprite touches the edge, <touching (edge v)?> becomes true, and the script below it is run.

Other Uses

A Boolean block can be used in a string input. If a Boolean in a String input is true, it reports "true". If the Boolean is false, it returns "false". For example, say <touching (mouse-pointer v)?> makes the sprite say "true" if the sprite is touching the mouse pointer, and "false" otherwise.

Direct Comparison

Boolean variables can be compared to non-Boolean variables. For example, the following script, two Booleans are compared to each other directly, and the if statement will execute the code inside if both have the same value (i.e. both true or both false).

if <<mouse down> = <touching color (#00A)>> then
...
end

Reporting Booleans

Sensor ()? is the only Boolean that can be displayed as a Stage Monitor. However, Booleans can be plugged into a Say () block to report its value.

when gf clicked
forever
say <touching (mouse-pointer v)?>

Storage in Variables

Booleans can be stored in variables as well. The following script stores the current mouse state in the variable "bool".

set [bool v] to <mouse down?>

Later, the variable can be compared to another Boolean with the stored variable:

if <<mouse down?> = (bool)> then
...
end

In this case, the script will check whether the mouse currently has the same state as it did when the variable was stored.

See Also

Cookies help us deliver our services. By using our services, you agree to our use of cookies.