Boolean Block

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

 File:The shape of a Boolean block.png
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.

### Sensing

* <sb><touching ( v)?></sb>
* <sb><touching color (#ff0000)?></sb>
* <sb><color (#00ff00) is touching (#0000ff)?></sb>
* <sb><key ( v) pressed?></sb>
* <sb><mouse down?></sb>

### Operators

* <sb><[] < []></sb>
* <sb><[] = []></sb>
* <sb><[] > []></sb>
* <sb><<> and <>></sb>
* <sb><<> or <>></sb>
* <sb><not <>></sb>
* <sb><[] contains []?::operators></sb>

### micro:bit

* <sb><( v) button pressed?::microbit></sb>
* <sb><tilted ( v)?::microbit></sb>

### LEGO BOOST

* <sb><seeing ( v) brick?::extension></sb>

### Go Direct Force and Acceleration

* <sb><tilted ( v)?::extension></sb>
* <sb><falling?::extension></sb>

### Removed

* <sb version="2.0">loud?:: sensing</sb>
* <sb version="2.0"><sensor [ v]?::extension></sb>

## 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:

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

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

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

In general, Booleans return string values of "true" or "false", but when inserted into a reporter input, the Boolean instead will return "1" or "0", allowing mathematical operations to be performed. For example, consider the script below:

<scratchblocks>
when gf clicked
forever

```
 say ((3) + <touching (Sprite1 v)?>)

```

end
</scratchblocks>

*If* the Boolean condition is true, the sprite will say the number "4". A true condition is an equivalent to "1". If the Boolean condition is false, the sprite will say the number "3". A false condition is equivalent to "0". Whether the numbers "1" and "0" are strings or numbers is not important in Scratch since the specification of variables types is unnecessary, unlike some other programming languages.

If the () + () block is ommitted entirely, the Boolean will then return "true" or "false" since the Boolean block would lie in a string input instead of a reporter input.

## 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:

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

The Wait Until () block pauses the script until the Boolean value, here <sb><touching (edge v)?></sb>, is true. While the sprite is not touching the edge, <sb><touching (edge v)?></sb> is false, and the block waits for it to become true. When the sprite touches the edge, <sb><touching (edge v)?></sb> 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, <sb>say <touching (mouse-pointer v)?></sb> 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).
<scratchblocks>
if <<mouse down> = <touching color (#00A)>> then
...
end
</scratchblocks>

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

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

### Storage in Variables

Booleans can be stored in variables as well. The following script stores the current mouse state in the variable "bool".
<scratchblocks>
set [bool v] to <mouse down?>
</scratchblocks>

Later, the variable can be compared to another Boolean with the stored variable:
<scratchblocks>
if <<mouse down?> = (bool)> then
...
end
</scratchblocks>
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

* Booleans on Wikipedia
* Custom Blocks
* Reporter Block

de:Blockform = Wahrheits-Blöcke
ja:真偽ブロック
es:Bloque lógico