Operators#

Introduction#

The Operator blocks in VEXcode AIR help the drone work with numbers and words. These blocks can solve math problems, compare values, evaluating if conditions are true or false, and working with strings. Strings can be used to print values and words.

Below is a list of available blocks:

  • math operator — Performs addition, subtraction, multiplication, or division.

  • comparison operator — Compares two values using greater than, less than, or equal to operators.

  • logical operator — Evaluates multiple conditions using and or or.

  • not operator — Inverts a Boolean value.

  • range operator — Checks if a number falls within a range.

  • pick random — Generates a random number within a specified range.

  • round number — Rounds a number to the nearest whole number.

  • math functions — Applies operations such as square root, trigonometry, logarithms, and exponentiation.

  • atan2 — Computes the inverse tangent of Y/X.

  • remainder — Reports the remainder of a division operation.

  • join – Combines two strings into one.

  • letter – Extracts a character from a string by position.

  • length – Reports the number of characters in a string.

  • contains – Checks if a string includes a specific word or character.

  • convert – Converts a number into text, a whole number, or a decimal.

math operator#

The math operator reporter block performs basic math operations on the values placed on either side of the operator. It returns a result based on the project’s print precision, which defaults to 0 decimal places but can be adjusted using the set print precision on screen or set print precision on console blocks.

The Math operator reporter block.#
    ([0] [math_plus v] [0])

Parameters

Description

value 1

The first number used in the operation.

operator

The mathematical operator to use:

  • + - Addition
  • - - Subtraction
  • * - Multiplication
  • / - Division

value 2

The second number used in the operation.

Example

Perform basic addition and show two decimal places.#
  when started
  [Perform basic addition and show two decimal places.]
  set print precision to [0.01 v] on console
  print ([10] [math_plus v] [10.5]) on console ▶

comparison operator#

The comparison operator Boolean block reports whether the comparison between two values is True or False.

  • True – The comparison is correct (e.g., 5 > 3 returns True).

  • False – The comparison is incorrect (e.g., 2 ≥ 5 returns False).

The Comparison operator Boolean block.#
    <[0] [math_equal v] [0]>

Parameter

Description

value 1

The first value to compare.

operator

The comparison to use:

  • = - Equal to
  • < - Less than
  • - Less than or equal to
  • > - Greater than
  • - Greater than or equal to

value 2

The second value to compare.

Example

  when started
  [Fly to position (0, 300, 500).]
  take off to [500] [mm v] ▶
  wait (1) seconds
  move [forward v]
  wait until <([y v] position in [mm v]) [math_greater_than v] [300]>
  wait (1) seconds
  land ▶

logical operator#

The logical operator Boolean block reports whether the logical condition between multiple values is True or False.

  • True – The logic condition is met.

  • False – The logic condition is not met.

The Logical Operator Boolean block.#
    <<> [and v] <>>

Parameter

Description

condition 1

The first Boolean condition to evaluate.

operator

The logical operator to use: and reports True if both conditions are True. or reports True if at least one condition is True.

condition 2

The second Boolean condition to evaluate.

Example

  when started
  [Display when buttons 5 and 7 are pressed together.]
  wait until <<controller button [5 v] pressed?> [and v] <controller button [7 v] pressed?>>
  print [Both buttons pressed!] on console ▶

not operator#

The not operator Boolean block inverts the result of a Boolean block. If the input is True, it reports False, and if the input is False, it reports True.

  • True – The input condition would normally report False.

  • False – The input condition would normally report True.

The Not Operator Boolean block.#
    <not <>>

Parameter

Description

condition

The Boolean condition to be inverted.

Example

  when started
  [Move with controller while button 5 is not pressed.]
  take off to [500] [mm v] ▶
  while <not <controller button [5 v] pressed?>>
    move with controller
  end
  land ▶

range operator#

The range operator Boolean block reports whether a value falls within a specified range.

  • True – If the value is within the range.

  • False – If the value is outside the range.

The Range Operator Boolean block.#
    <[0] [math_less_than v] [0] [math_less_than v] [0]>

Parameter

Description

first operator

The comparison operator to use:

  • < — Less than
  • — Less than or equal to
  • > — Greater than
  • — Greater than or equal to

value

The value to check.

second operator

The comparison operator to use:

  • < — Less than
  • — Less than or equal to
  • > — Greater than
  • — Greater than or equal to

Example

  when started
  [Hover at approximately 1000mm then land.]
  take off to [500] [mm v] ▶
  climb [up v]
  wait until <[1000] [math_less_than v] (altitude in [mm v]) [math_less_than v] [1100]>
  hover
  wait (3) seconds
  land ▶

pick random#

The pick random reporter block reports a random positive or negative number between a minimum and a maximum. It can return both the min value and the max value. For example, picking 1 to 10 can return any whole number from 1 through 10.

The Pick Random reporter block.#
    pick random [1] to [10]

Parameters

Description

min

The lowest possible number that can be picked.

max

The highest possible number that can be picked.

Examples

Display a number between 1 and 10.#
  when started
  [Display a number between 1 and 10.]
  print (pick random [1] to [10]) on console ▶

Display a decimal number between 1 and 10.5.#
  when started
  [Display a decimal number between 1 and 10.5.]
  set print precision to [All Digits v] on console
  print (pick random [1] to [10.5]) on console ▶

round number#

The round number reporter block reports the given number rounded to the nearest whole number, following standard rounding rules:

  • If the decimal is 0.5 or greater, the number rounds up.

  • If the decimal is less than 0.5, the number rounds down.

The Round Number reporter block.#
    (round [0] to [0] decimal places)

Parameter

Description

number

The number to round.

decimal places

The amount of decimals places to round to.

Example

Display the rounded result of 10 / 3.#
  when started
  [Display the rounded result of 10 / 3.]
  set print precision to [0.01 v] on console
  print (round ([10] [math_division v] [3]) to [1] decimal places) on console ▶

math functions#

The math functions reporter block applies a selected mathematical function to a given number and returns the result. It supports operations such as absolute value, rounding, square roots, trigonometric functions, logarithms, and exponentiation.

The Math Functions reporter block.#
    [abs v] of [0]

Parameter

Description

function

The mathematical operation to apply to the input value:

  • abs — Absolute value
  • floor — Rounds down
  • ceiling — Rounds up
  • sqrt — Square root
  • sin — Sine
  • cos — Cosine
  • tan — Tangent
  • asin — Inverse sine
  • acos — Inverse cosine
  • atan — Inverse tangent
  • ln — Natural logarithm
  • log — Base 10 logarithm
  • e^ — Euler’s number raised to a power
  • 10^ — 10 raised to a power
  • negative — Returns the negative of the number

number

The number to apply the function to.

Example

Display the square root of 16.#
  when started
  [Display the square root of 16.]
  print ([sqrt v] of [16]) on console ▶

atan2#

The atan2 reporter block reports the inverse tangent of Y/X as an angle in degrees, representing the angle between the positive X-axis and the point (X, Y).

atan2 block#
  (atan2 of x:[4] y:[3])

Parameter

Description

x

The horizontal position of the point, measured from the origin (0,0).

y

The vertical position of the point, measured from the origin (0,0).

Example

Calculate the angle from current position (4, 3).#
  when started
  [Calculate the angle from current position (4, 3).]
  print (atan2 of x:[4] y:[3]) on console ▶

remainder#

The remainder reporter block reports the remainder of the division operation between two values.

The Remainder reporter block.#
    remainder of [0] / [0]

Parameter

Description

dividend

The number to be divided.

divisor

The number to divide by.

Example

Display the remainder of 10 / 3.#
  when started
  [Display the remainder of 10 / 3.]
  print (remainder of [10] / [3]) on console ▶

join#

The join reporter block reports a single string that combines two input strings.

The Join reporter block.#
    (join [apple] [banana])

parameter

description

string 1

The first string to combine.

string 2

The second string to combine.

Example

  when started
  [Display "VEXcode" on the Console.]
  print (join [VEX] [code]) on console ▶

letter#

The letter reporter block reports a single-character string from the specified position in the given string.

The Letter reporter block.#
    (letter [1] of [apple])

parameter

description

position

The position of the character in the string (starting at 1).

string

The string to extract a letter from.

Example

  when started
  [Display the first letter of "drone".]
  print (letter (1) of [drone]) on console ▶

length#

The length reporter block reports the number of characters in the specified string, including spaces.

The Length reporter block.#
    (length of [apple])

parameter

description

string

The string to measure the length of.

Example

  when started
  [Count the number of characters in "VEX Robotics".]
  print (length of [VEX Robotics]) on console ▶

contains#

The contains Boolean block reports whether the specified string includes the given word or character.

  • True – The string includes that specific word or character.

  • False – The string does not include that specific word or character.

The Contains Boolean block.#
    <[apple] contains [a] ?>

parameter

description

string

The main string to search within.

search term

The word or character to check for inside the string.

Example

  when started
  [Check if "robotics" contains "bot".]
  if <[robotics] contains [bot] ?> then
    print [The word contains "bot".] on console ▶
  else
    print [Not found.] on console ▶
  end

convert#

The convert reporter block reports the value of a number converted to the specified type, either as text or a number.

  • text – Converts the number to a string. Numbers must be in string format to work with String Operator blocks.

  • number – Converts the number to a decimal value.

The Convert reporter block.#
    (convert [0] to [text v])

parameter

description

value

The number to convert.

type

The type to convert the number into:

  • text
  • number

Example

  when started
  [Add any number to 5.]
  ask [Give me a number.] and wait
  print ((convert (answer) to [number v]) [math_plus v] [5]) on console ▶