(Redirected from Boolean)

The general shape of a Boolean block.
A Boolean block is an elongated hexagonal block that reports boolean values. 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 their usage in a script.

There are 13 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 13 Boolean blocks in Scratch 3.0. Click on a block for more information.

## 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
end
```

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

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

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:

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

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.

## Types

Scratch has two types of booleans: booleans that check a specific condition and comparative Booleans.

Conditional booleans check a specific condition. Examples include the () Contains () and Touching ()? blocks.

Comparative booleans compare values. These include () = (), () and (), and Not (). For some of these, a truth table will predict their return values.

## 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) secs
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, it can be plugged into a Say () block to report the 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.