Variables#

Introduction#

The Variable blocks in VEXcode EXP let you store information that your project can use later. A variable is like a labeled container that holds a value, such as a number, a Boolean, or a list of items.

You would use a variable when you want your project to remember something while it is running. For example, a variable could keep track of how many times the robot has moved, how many objects it has counted, or whether a goal has been reached.

Variables are helpful when the information in a project might change. Instead of writing the same number or answer in many places, you can store it in a variable and update it as the project runs. This makes your project easier to change, check, and understand.

In VEXcode EXP, Variable blocks can store different kinds of information, including numbers, Boolean values such as true or false, lists, and 2D lists. These blocks are useful when a project needs to count, make decisions, track progress, or organize information.

Below is a list of available blocks:

Numeric Variables - Store and modify numbers.

Boolean Variables – Store True or False values.

Lists – Store and manage collections of values.

2D Lists – Store and manage collection of values in a grid.

Numeric Variables#

numeric variable#

The numeric variable reporter block reports the numeric value assigned to the variable.

The Numeric variable reporter block.#
    (myVariable)

Parameter

Description

This block has no parameters.

Example

  when started
  [Display the value of the variable.]
  set [deliveries v] to [2]
  print (deliveries) on screen ▶

set numeric variable#

The set numeric variable stack block assigns a value to a numeric variable.

The set Numeric variable block.#
    set [myVariable v] to [1]

Parameter

Description

variable

The numeric variable to assign a value to. Users can select an existing variable, rename it, or delete it.

value

The number assigned to the variable (can be a whole number, decimal, or numeric block).

Example

  when started
  [Display the value of the variable.]
  set [deliveries v] to [2]
  print (deliveries) on screen ▶

change numeric variable#

The change numeric variable stack block increases or decreases a variable’s value by a given amount.

The Change Numeric Variable stack block.#
    change [myVariable v] by (1)

Parameter

Description

variable

The numeric variable to modify. Users can select an existing variable, rename it, or delete it.

value

The amount to increase or decrease the variable by.

Example

  when started
  [Deliver a package twice.]
  set [deliveries v] to [0]
  drive [forward v] for (100) [mm v] ▶
  change [deliveries v] by [1]
  drive [forward v] for (100) [mm v] ▶
  change [deliveries v] by [1]
  print (deliveries) on screen ▶

Boolean Variables#

Boolean variable#

The Boolean variable Boolean block reports either a True or False value.

The Boolean Variable reporter block.#
    <myBoolean>

Parameter

Description

This block has no parameters.

Example

  when started
  [Display a message if the variable is set to True.]
  set [myBoolean v] to <true v>
  if <myBoolean> then
  print [Boolean is True!] on screen ▶
  end

set Boolean variable#

The set Boolean variable stack block assigns a True or False value to a Boolean variable.

The Set Boolean Variable stack block.#
    set [myBoolean v] to <true v>

Parameter

Description

variable

The Boolean variable to assign a value to. Users can select an existing variable, rename it, or delete it.

value

A Boolean value (True or False).

Example

  when started
  [Repeat until 3 deliveries are made.]
  set [finished v] to <false v>
  set [deliveries v] to [0]
  repeat until <finished>
    drive [forward v] for (100) [mm v] ▶
    change [deliveries v] by [1]

    if <(deliveries) [math_equal v] [3]> then
      set [finished v] to <true v>

Lists#

list item#

The list item reporter block reports the value of an item in a list. A list is a variable that can store multiple variables of the same type.

The List Item reporter block.#
    (item (1) of [myList v])

Parameter

Description

index

The position of the item in the list (starting at 1).

variable

The list variable to report an item from. Users can select an existing list, rename it, or delete it.

Example

  when started
  [Display the delivery locations.]
  set [delivery_spots v] to [Red House] [Green House] [Blue House]
  drive [forward v] for (100) [mm v] ▶
  print (item (1) of [delivery_spots v]) on screen ▶
  drive [forward v] for (100) [mm v] ▶
  print (item (2) of [delivery_spots v]) on screen ▶
  drive [forward v] for (100) [mm v] ▶
  print (item (3) of [delivery_spots v]) on screen ▶

replace list item#

The replace list item stack block updates a specific item in a list.

The Replace List Item stack block.#
    replace item [1] of [myList v] to [1]

Parameter

Description

index

The position of the item in the list (starting at 1).

variable

The list variable containing the item to modify. Users can select an existing list, rename it, or delete it.

value

The new value assigned to the specified list item.

Example

  when started
  [Replace the first item in a list with 4.]
  set [myList v] to (5) (1) (9)
  replace item (1) of [myList v] to (4)
  print (item (1) of [myList v]) on screen ▶

set list items#

The set list items stack block sets every item in the list to the entered values.

The Set List Items stack block.#
    set [myList v] to (0) (0) (0)

Parameter

Description

variable

The list variable being assigned values. Users can select an existing list, rename it, or delete it.

values

The values to store in the list, entered as individual items.

Example

  when started
  [Display the last item in a list.]
  set [myList v] to (1) (2) (3)
  print (item (3) of [myList v]) on screen ▶

length of list#

The length of list reporter block reports the number of items in a list.

The Length of List reporter block.#
    (length of [myList v] :: list)

Parameter

Description

variable

The list variable to report the length of. Users can select an existing list, rename it, or delete it.

Example

  when started
  [Display how many items are in a list.]
  set [myList v] to (1) (2) (3)
  print (length of [myList v] :: list) on screen ▶

2D Lists#

2D list item#

The 2D list item reporter block reports the value of an item in a 2D list. 2D lists are variables that store multiple list variables.

The 2D List Item reporter block.#
    (item (1) (1) of [my2DList v])

Parameter

Description

row

The row of the item (starting at 1).

column

The column of the item (starting at 1).

variable

The 2D list variable to report an item from. Users can select an existing 2D list, rename it, or delete it.

Example

  when started
  [Display an item from a 2D list.]
  set [my2DList v] to ([1][7]) ([2][8]) 
  print (item (2) (2) of [my2DList v]) on screen ▶

replace 2D list item#

The replace 2D list item stack block updates a specific item in a 2D list.

The Replace 2D List Item stack block.#
    replace item (1) (1) of [my2DList v] to (1)

Parameter

Description

row

The row index of the item (starting at 1).

column

The column index of the item (starting at 1).

variable

The 2D list variable containing the item to modify. Users can select an existing 2D list, rename it, or delete it from the drop-down menu.

value

The new value assigned to the specified 2D list item.

Example

  when started
  [Replace a value in a 2D list and print the updated item.]
  set [my2DList v] to ([1][2]) ([3][4]) 
  replace item (2) (2) of [my2DList v] to (25)
  print (item (2) (2) of [my2DList v]) on screen ▶

set 2D list items#

The set 2D list items stack block sets every item in the 2D list to the entered values.

The Replace 2D List Item stack block.#
    set [my2DList v] to ([0][0]) ([0][0])

Parameter

Description

variable

The 2D list variable being assigned values. Users can select an existing 2D list, rename it, or delete it.

values

The values to store in the 2D list, entered row by row.

Example

  when started
  [Display an item from a 2D list.]
  set [my2DList v] to ([1][4]) ([2][15]) 
  print (item (1) (2) of [my2DList v]) on screen ▶

length of 2D list#

The length of 2D list reporter block reports the number of rows or columns in a 2D list.

The Length of 2D List reporter block.#
    (length of [my2DList v] [rows v])

Parameter

Description

variable

The 2D list variable to check the length of. Users can select an existing 2D list, rename it, or delete it.

rows/columns

Whether to report the number of rows or columns.

Example

  when started
  [Set a 2D list and print the number of rows it has.]
  set [my2DList v] to ([1][5]) ([3][6]) 
  print (length of [my2DList v] [rows v]) on screen ▶