If Then Blocks#

if / else blocks are a way for your robot to make decisions during a project.

Understanding if / else blocks#

The if then branch#

An if/else block has two parts. In the if then branch, you insert a Boolean block into the condition slot for the robot to check.

The if/else block's shape, with the if then branch highlighted.

The else branch#

In the else branch, you add the blocks you want to run when the condition is false.

The if/else block's shape, with the else branch highlighted.

Insert a Boolean block#

Drag a Boolean block into the empty condition slot in the if then branch.

A controller ▲ button pressed Boolean block being dragged into the empty condition slot of an if/else block.

All Boolean blocks have sharp edges and a question mark in its name, such as the controller button pressed? block.

An if/else block with a controller ▲ button pressed Boolean block, with the Boolean block highlighted.

If the condition is true#

If the condition is true, the blocks inside the if then branch will run.

An if/else block with a controller ▲ button pressed Boolean block and a drive forward block, with the drive forward block highlighted.

If the condition is false#

If the condition is false, the blocks inside the else branch will run.

An if/else block with a controller ▲ button pressed Boolean block, a drive forward block, and a stop driving block, with the stop driving block highlighted.

Repeating if / else blocks#

An if/else block only checks its condition once. If you want the condition to be checked repeatedly, put the if/else block inside a looping block, like forever.

An if/else block nested inside a forever block.#
por siempre
si <¿[Controller1 v] [▲ v] presionado?> entonces
conducir [adelante v]
si no
detener conducción
fin
fin

The if Block#

The if block is only the if then branch. If the condition isn’t true, nothing will happen.

The if block.#
si <¿[Controller1 v] [▲ v] presionado?> entonces
conducir [adelante v]
fin

The if / else if / else Block#

The if / else if / else block can be used to test multiple conditions one after another. It can be expanded to test as many conditions as needed.

An if/else if/else block, testing three Controller buttons.#
si <¿[Controller1 v] [◀ v] presionado?> entonces
girar [izquierda v] por (90) grados ▶
si no, si <¿[Controller1 v] [▶ v] presionado?> entonces
girar [derecha v] por (90) grados ▶
si no, si <¿[Controller1 v] [A v] presionado?> entonces
detener conducción
si no
conducir [adelante v]
fin