Controls#

Introduction#

The Control blocks in VEXcode AIM manage the flow of a project by handling loops, conditions, delays, and stopping execution. Below is a list of available blocks:

  • wait – Pauses execution for a specific duration.

  • wait until – Pauses execution until a specified condition is met.

  • repeat – Repeats enclosed blocks a specific number of times.

  • forever – Repeats enclosed blocks indefinitely.

  • repeat until – Repeats enclosed blocks until a condition is met.

  • while – Repeats enclosed blocks while a condition remains true.

  • if – Runs enclosed blocks if a condition is true.

  • if / else – Runs one set of blocks if a condition is true, otherwise runs another.

  • if / else if / else – Runs different sets of blocks depending on multiple conditions.

  • break – Exits a repeat, forever, repeat until, or while loop immediately.

  • stop project – Ends the execution of the project.

wait#

The wait block pauses for a specific amount of time before moving to the next block.

wait stack block#
    wait (1) seconds

Parameters

Description

time

The amount of time to wait, as a positive integer or decimal in seconds.

Example

When started, moves forward for one second, then stops all movement.#
    when started :: hat events
    [Move forward for one second, then stop.]
    move [forward v]
    wait (1) seconds
    stop all movement

wait Until#

The wait until block pauses execution until a specified condition is met before proceeding to the next block.

wait until stack block#
    wait until <>

Parameters

Description

condition

The condition that must be met before moving on to the next block.

Example

When started, moves forward until the screen is pressed, then stops all movement.#
    when started :: hat events
    [Move forward until screen pressed, then stop.]
    move [forward v]
    wait until <screen pressed?>
    stop all movement

repeat#

The repeat block runs the blocks inside it a set number of times.

repeat c block#
    repeat [10]
    end

Parameters

Description

times

A whole number that sets how many times the repeat block runs.

Example

    when started :: hat events
    [Move in a square path.]
    repeat [4]
        move [forward v] for [50] [mm v] ▶
        turn [right v] for [90] degrees ▶
    end

forever#

The forever block keeps running the blocks inside it again and again without stopping.

forever c block#
    forever
    end

Parameters

Description

This block has no parameters.

Example

When started, continuously moves in a square path by moving forward 50 mm and turning right 90 degrees in an infinite loop.#
    when started :: hat events
    [Move in a square path forever.]
    forever
        move [forward v] for [50] [mm v] ▶
        turn [right v] for [90] degrees ▶
    end

repeat until#

The repeat until block executes the enclosed blocks repeatedly while the condition evaluates as False.

repeat until block#
    repeat until <>
    end

Parameters

Description

condition

An expression or variable that is evaluated before each iteration. If it evaluates as:

  • False - The loop continues
  • True - The loop stops

Example

When started, keeps all LEDs red until the screen is pressed, then turns them off.#
    when started :: hat events
    [Repeat until the screen is pressed before turning off the LEDs.]
    repeat until <screen pressed?>
        set [lightall v] LED color to [red v]
    end
    set [lightall v] LED color to [off v]

while#

The while block executes the enclosed blocks repeatedly while the condition evaluates as True.

while c block#
    while <>
    end

Parameters

Description

condition

An expression or variable that is evaluated before each iteration. If it evaluates as:

  • True - The loop continues
  • False - The loop stops

Example

When started, moves forward for 200 mm while keeping all LEDs green. The LEDs turn off after movement stops.#
    when started :: hat events
    [Keep the LEDs green while the robot is moving.]
    move [forward v] for [200] [mm v] ◀ and don't wait
    while <move active?>
        set [lightall v] LED color to [green v]
    end
    set [lightall v] LED color to [off v]

if#

The if block executes the enclosed block of code if the condition evaluates as True.

if block#
    if <> then
    end

Parameters

Description

condition

An expression or variable that is evaluated when the statement runs. If it evaluates as:

  • True - The code inside the if block executes
  • False - The block is skipped

Example

When started, continuously checks if the screen is pressed. If pressed, the robot kicks an object with medium force.#
    when started :: hat events
    [Kick when the screen is pressed.]
    forever
        if <screen pressed?> then
            kick object [medium v]
        end
    end

if / else#

The if / else block determines which enclosed block of code runs based on whether the condition evaluates as True or False.

if then else block#
    if <> then
    else
    end

Parameters

Description

condition

An expression or variable that is evaluated when the statement runs. If it evaluates as:

  • True - The code inside the if block executes
  • False - The code inside the else block executes instead

Example

When started, continuously checks if the screen is pressed. If pressed, displays an excited emoji looking forward; otherwise, displays a bored emoji looking forward.#
    when started :: hat events
    [Show one emoji when the screen is touched and a different one when it's not.]
    forever
        if <screen pressed?> then
            show [emoji_excited v] looking [forward v]
        else
            show [emoji_bored v] looking [forward v]
        end
    end

if / else if / else#

The if / else if / else block structure selects which enclosed block of code runs based on conditions:

  • if runs its block of code if the condition evaluates as True.

  • else if checks additional conditions only if all previous conditions evaluated as False. Multiple else if statements can be used.

  • else runs its block of code only if none of the previous conditions evaluated as True.

if else if else block#
    if <> then
    else if <> then
    else 
    end

Parameters

Description

condition

An expression or variable that is evaluated when the statement runs. The first condition that evaluates as:

  • True - Determines which block of code executes
  • False - If none are True, the else block of code runs

Example

When the controller’s axis 1 position changes, moves forward if pushed forward, moves in reverse if pulled backward, and stops when neutral.#
    when Controller axis [1 v] is changed :: hat events
    [Move the robot forward or reverse based on the position of the joystick.]
    if <(controller axis [1 v] position) [math_greater_than v] [0]> then
        move [forward v]
    else if <(controller axis [1 v] position) [math_less_than v] [0]> then
        move [reverse v]
    else
        stop all movement
    end

break#

The break block exits a loop immediately.

break stack block#
    break

Parameters

Description

This block has no parameters.

Example

When started, flashes LEDs red and green every 0.5 seconds until the screen is pressed, then stops.#
    when started :: hat events
    [Flash LEDs until the screen is pressed.]
    forever
        set [lightall v] LED color to [red v]
        wait [0.5] seconds
        set [lightall v] LED color to [green v]
        wait [0.5] seconds
        if <screen pressed?> then
            break
        end
    end

stop project#

The stop project block ends a running project.

stop project block#
    stop project

Parameters

Description

This block has no parameters.

Example

When started, flashes LEDs red and green every 0.5 seconds until the screen is pressed, then stops the project.#
    when started :: hat events
    [Stop the project once the screen is pressed.]
    forever
        set [lightall v] LED color to [red v]
        wait [0.5] seconds
        set [lightall v] LED color to [green v]
        wait [0.5] seconds
        if <screen pressed?> then
            stop project
        end
    end