Operators

Types of Operators

Operators are used in expressions. Operators can be binary and unary:
  • Binary: Most operators are binary, this means that they should be used with two operands. For example operator + can be used like:
    a = 2 + 3
    
  • Unary: Unary operators are used on a single operand. There is only one unary operator NOT in Toy Basic:
    if not a > 5
    	' ...
    end if
    
There are arithmetic, string, comparison and logic operators in Toy Basic:
  • Arithmetic operators can only be performed between numeric operands. If they are used with string operands, string operands are converted to numeric first:
    a = 2 + "3" ' a = 5, because "3" is converted to 3
    b = 2 + "three" ' b = 2, because "three" is not a number and it is converted to 0
    
  • String operators can only be performed between string operands. If they are used with numeric operands, numeric operands are converted to strings first:
    Text = "Value is: " & 3 ' Text = "Value is: 3",  because 3 is converted to "3"
    
  • Comparison operators. They are usually used in if and while statements
    if a < 10
    	' ...
    end if
    
  • Logical operators. They are also usually used in if and while statements to combine comparison operations.
    if a < 10 and a > 5
    	' ...
    end if
    

List of Operators

Arithmetic operators in Toy Basic:

OperatorExampleDescription
+
a = b + 6
Addition - adds two numbers together
-
a = b - 6
Subtraction - subtracts the second number from the first one
*
a = b * 6
Multiplication - multiplies two numbers together
/
a = b / 6
Division - divides the first number by the second one
%
a = b % 6
Modulus - returns the remainder of dividing the first number by the second one
^
a = b ^ 6
Exponentiation - raises the value to the first number to the power of the second one

String operators in Toy Basic:

OperatorExampleDescription
&
s = "Hello " & name
Concatenation - join two strings into a single string

Comparison operators in Toy Basic

OperatorExampleDescription
=
if a + 1 = 5
Equality - Is the value of the first expression equal to the value of the second? You can compare numbers and strings, but if you compare a string to a number, number is converted to strings
<>
if a + 1 <> 5
Inquality - Is the value of the first expression unequal to the value of the second? You can compare only numbers, strings are always converted to numbers for comparison.
<
if a + 1 < 5
Less than - Is the value of the first expression less than the value of the second? You can compare only numbers, strings are always converted to numbers for comparison.
>
if a + 1 > 5
Greater than- Is the value of the first expression greater than the value of the second? You can compare only numbers, strings are always converted to numbers for comparison.
<=
if a + 1 <= 5
Less than or equal to - Is the value of the first expression less than or equal to the value of the second? You can compare only numbers, strings are always converted to numbers for comparison.
>=
if a + 1 >= 5
Greater than or equal to - Is the value of the first expression greater than or equal to the value of the second? You can compare only numbers, strings are always converted to numbers for comparison.

Logical operators in Toy Basic

OperatorExampleDescription
AND
if a > 5 and a < 10
Condition is met, when both expressions are true
OR
if a > 5 or a < 10
Condition is met, when at least one expression is true
NOT
if not a > 5
Remember, NOT is an unary operator. Condition is met, expression is NOT true

Priority or order of Operators

  • Expressions in brackets are evaluated first:
    a = 2 * (3 + 4) ' a = 14
    
  • Exponentiation operator ^ has the highest priority:
    a = 2 * 3 ^ 2 ' a = 18
    
  • Arithmetic operators *, / and % have the next priority:
    a = 2 + 4 / 2 ' a = 4
    
  • Arithmetic and string operators +, - and & have the next priority:
    if 3 + 4 < 1 + 7 ' condition is true
    
  • Comparison operators =, <>, <, >, <= and >= have the next priority:
    if not 2 > 10 ' condition is true
    
  • Unary logical operator NOT has the next priority:
    if not 2 > 10 and 3 < 5' condition is true
    
  • Logical operators AND and OR have the lowest priority:
    if not 2 < 10 or 3 < 5' condition is true
    

    See also: