Logic

Basic logical blocks

../../../_images/image-115.png

Branch blocks

If condition* is true, perform the action

../../../_images/image-210.png

If condition* is true, perform an action, else perform another action

../../../_images/image-39.png

Note

* Boolean condition - an expression, the result of which is “true” or “false”

You can modify these blocks by adding more conditions to them

../../../_images/image-14.gif

Logic AND and OR

../../../_images/image-46.png

If at least one of the simple statements connected by the AND operation is false, then the compound statement will also be false

If at least one of the simple statements connected by the OR operation is true, then the compound statement will also be true

Comparison operations

Usually used with numbers. Equality operations can also be used on text

../../../_images/image-54.png

You can only compare data of the same type (you can’t compare numbers and text)

True/False

Boolean variable

../../../_images/image-65.png

Logical NOT

The logical operation does NOT invert the statement behind it

../../../_images/image-74.png

For example, the following expression (however useless but cognitive) will return true

../../../_images/image-82.png

Ternary conditional operation

../../../_images/image-101.png

A ternary operation can be defined as an operation that returns its second expression depending on the value of the logical expression given by the first expression. The operation algorithm is as follows:

  1. The boolean expression is evaluated
  2. If the logical expression is true, then the value of expression expression 1 is evaluated; otherwise, the value of expression expression 2 is evaluated
  3. The computed value is returned

Note

Only one of the expressions is evaluated: expression 1 or expression 2. This corresponds to the principle of lazy evaluation

Example

../../../_images/image-116.png
  1. If both simple buttons are pressed, then change “color” to “blue”, otherwise - “red”
  2. Set a simple light bulb to the color stored in the “color” variable

Which is equivalent to the following construction

../../../_images/image-121.png

True with probability

../../../_images/image-131.png

Boolean variable whose truth will depend on the specified probability