From Test-Scratch-Wiki

(Redirected from Eng:Boolean)


The general shape of a boolean block.

A Boolean block is an elongated hexagonal block shaped after the boolean elements in flowcharts. A boolean block contains a condition. When the block is invoked, 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 2.0, as follows:

In Scratch 3.0, one new Boolean block will be added:

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 in string inputs:

when gf clicked
forever
say <touching [Sprite1 v]?>

They can fit in number (reporter) inputs, too. 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.

Use

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 () 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.

History

In Scratch 0.1, there was a drop-down menu with the values "true" and "false".

The old menu

External Links

See Also


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