Operators#
Introduction#
The Operators blocks in VEXcode IQ (2nd gen) handle mathematical calculations and string manipulations. These blocks allow for performing arithmetic, evaluating conditions, and processing text. Below is a list of available blocks:
addition operator – Adds two numbers.
subtraction operator – Subtracts one number from another.
multiplication operator – Multiplies two numbers.
division operator – Divides one number by another.
pick random – Generates a random number within a specified range.
greater than operator – Returns True if the first number is greater than the second.
less than operator – Returns True if the first number is less than the second.
equal to operator – Returns True if the two values are equal.
and operator – Returns True if both conditions are True.
or operator – Returns True if at least one condition is True.
not operator – Inverts the result of a Boolean condition.
round number – Rounds a number to the nearest whole number.
math functions – Applies operations such as square root, trigonometry, logarithms, and exponentiation.
remainder – Returns the remainder of a division operation.
atan2 – Computes the inverse tangent of Y/X.
addition operator#
The addition operator block returns the sum of two numbers.
([0] + [0])
Parameters |
Description |
---|---|
number 1 |
The first number to add. |
number 2 |
The second number to add. |
Example
when started :: hat events
[Display the sum of two numbers.]
print ([2] + [2]) on [Brain v] ▶
subtraction operator#
The subtraction operator block returns the result of subtracting the second number from the first.
([0] - [0])
Parameters |
Description |
---|---|
number 1 |
The number to subtract from. |
number 2 |
The number to subtract. |
Example
when started :: hat events
[Display the difference of two numbers.]
print ([3] - [2]) on [Brain v] ▶
multiplication operator#
The multiplication operator block returns the product of two numbers.
([0] * [0])
Parameters |
Description |
---|---|
number 1 |
The first number to multiply. |
number 2 |
The second number to multiply. |
Example
when started :: hat events
[Display the product of two numbers.]
print ([4] * [2]) on [Brain v] ▶
division operator#
The division operator block returns the result of dividing the first number by the second.
([0] / [0])
Parameters |
Description |
---|---|
number 1 |
The dividend. |
number 2 |
The divisor. |
Example
when started :: hat events
[Display the quotient of two numbers.]
print ([4] / [2]) on [Brain v] ▶
pick random#
The pick random block returns a random number, either an integer or a decimal, within the specified range.
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
when started :: hat events
[Display a number between 1 and 10.]
print (pick random [1] to [10]) on [Brain v] ▶
greater than operator#
The greater than operator block returns True if the first value is greater than the second, otherwise it returns False.
<[0] > [50]>
Parameters |
Description |
---|---|
number 1 |
The first value to compare. |
number 2 |
The second value to compare. |
Example
when started :: hat events
[Turn until the robot rotates more then 180 degrees.]
turn [right v]
wait until <([BrainInertial v] heading in degrees) > [180]>
stop driving
less than operator#
The less than operator block returns True if the first value is less than the second, otherwise it returns False.
<[0] < [50]>
Parameters |
Description |
---|---|
number 1 |
The first value to compare. |
number 2 |
The second value to compare. |
Example
when started :: hat events
[Turn while the robot is facing less than 180 degrees.]
while <([BrainInertial v] heading in degrees) < [180]>
turn [right v]
end
stop driving
equal to operator#
The equal to operator block returns True if both values are equal, otherwise it returns False.
<[0] = [0]>
Parameters |
Description |
---|---|
number 1 |
The first value to compare. |
number 2 |
The second value to compare. |
Example
when started :: hat events
[Print on a new line until the current row is set to 4.]
forever
print [Line] on [Brain v] ◀ and set cursor to next row
if <(cursor row) = [4]> then
break
end
end
and operator#
The and operator block returns True only if both conditions are True.
<<> and <>>
Parameters |
Description |
---|---|
condition 1 |
The first Boolean condition to evaluate. |
condition 2 |
The second Boolean condition to evaluate. |
Example
when started :: hat events
[Move forward if two buttons are pressed at the same time.]
forever
if <<Brain [Left v] button pressed?> and <Brain [Right v] button pressed?>> then
drive [forward v] for (200) [mm v] ▶
end
or operator#
The or operator block returns True if at least one of the two conditions is True.
<<> or <>>
Parameters |
Description |
---|---|
condition 1 |
The first Boolean condition to evaluate. |
condition 2 |
The second Boolean condition to evaluate. |
Example
when started :: hat events
[Move forward if either of two buttons are pressed.]
forever
if <<Brain [Left v] button pressed?> or <Brain [Right v] button pressed?>> then
drive [forward v] for (200) [mm v] ▶
end
not operator#
The not operator block inverts the result of a Boolean block. If the input is True, it returns False, and if the input is False, it returns True.
True – The input condition would normally return False.
False – The input condition would normally return True.
<not <>>
Parameter |
Description |
---|---|
condition |
The Boolean condition to be inverted. |
Example
when started :: hat events
[Turn right unless a button is pressed, then turn left.]
forever
if <not <Brain [Left v] button pressed?>> then
turn [right v]
else
turn [left v]
end
round number#
The round number block returns 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.
(round [0])
Parameter |
Description |
---|---|
number |
The number to round. |
decimal places |
The amount of decimals places to round to. |
Example
when started :: hat events
[Display the result of 10 / 3 with and without rounding.]
set print precision to [0.1 v] on screen
print [No Rounding: ] on [Brain v] ▶
print ([10] / [3]) on [Brain v] ◀ and set cursor to next row
print [Rounding: ] on [Brain v] ▶
print (round ([10] / [3])) on [Brain v] ▶
math functions#
The math functions 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.
[abs v] of [0]
Parameter |
Description |
---|---|
function |
The mathematical operation to apply to the input value:
|
number |
The number to apply the function to. |
Example
when started :: hat events
[Display the square root of 16.]
print ([sqrt v] of [16]) on [Brain v] ▶
remainder#
The remainder block returns the remainder of the division operation between two values.
remainder of [0] / [0]
Parameter |
Description |
---|---|
dividend |
The number to be divided. |
divisor |
The number to divide by. |
Example
when started :: hat events
[Display the remainder of 10 / 3.]
print (remainder of [10] / [3]) on [Brain v] ▶
atan2#
The atan2 block returns 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 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
when started :: hat events
[Calculate the angle from current position (4, 3).]
print (atan2 of x: [4] y: [3]) on [Brain v] ▶