Expressions

Expressions are operations in algebraic format that yield a value when evaluated.

An expression consists of operators and operands. Operators are special symbols commonly used in expressions, denoting the operations to be performed with the operands they are adjacent to. Operands can be variables or function invocations which return a value that can be operated on by the relevant operators

Expressions must operate on compatible variable types. Type checking is performed at compile time to guarantee that no runtime errors occur due to an invalid mix of types. Some expression examples follow.

Expressions with numerical values:
//Variable c is assigned the sum of a and b.
c = a + b
// myVariable is assigned the product of 12 by the sum of yourVariable and ourVariable.
myVariable = 12 * (yourVariable + ourVariable)
Expressions with string values:
employeeName = firstName + " " + lastName

Precedence

Notice the parentheses in the expression example above. Parentheses play a role in precedence. Precedence is the order in which operations take place in a mathematical equation. This is sometimes called order of operations. Any operation inside parentheses is evaluated first, and then followed by other operations.

If you evaluate the example:
myVariable = 12 * (yourVariable + ourVariable)
using 10 for yourVariable and 5 for ourVariable, the order of the operation is the following:
  1. yourVariable is added to myVariable resulting in 15.
  2. 15 is multiplied by 12 resulting in 180.
  3. myVariable is assigned the value 180.

For further information on the different operators, please see Operators.

Conditional expressions

Conditional expressions assign a value to a variable depending on the result of an expression. The format of a conditional expression is as follows:
<Boolean expression> ? <expression> : <expression>
If the Boolean expression evaluates to true, the value to the left of the colon is assigned. If it evaluates to false, the value to the right of the colon is assigned. Now, look the next example.
myResult = (show = 1) ? "on" : "off"

In this example, the variable myResult is assigned the value "on" if the value of the variable show is equal to 1. Otherwise, "off" is assigned to myResult.