# Using Operators in Expressions

An operator specifies a type-specific operation. Each operator combines with one or two expression terms, also called operands. For most operators, each operand can be a distinct expression.

The following sections describe in detail how these operators and their operands participate in an expression.

## Using Arithmetic Operators

G2 uses the following reserved characters to signify arithmetic operators:

Tip: For G2 to interpret the - (hyphen) character as an operator, rather than as a character in a symbol, include at least one space before and after it.

### Identifying the Default Order of Evaluation

By default, G2 evaluates an expression with an arithmetic operator from left to right. For example, G2 evaluates this expression:

as follows:

### Using Parentheses to Affect the Order of Evaluation

You can use parentheses to override the default order in which G2 evaluates an expression that includes an arithmetic operator. In the following expression, enclosing the middle two terms and their operator in parentheses changes how G2 evaluates the expression:

In this case, G2 evaluates this expression as follows:

### Precedence of Arithmetic Operators

When evaluating expressions that include arithmetic operators, G2 observes the following precedence among operators.

because the * (multiplication) operator has higher precedence than the + (addition) operator, G2 evaluates the entire expression as if its terms are combined within parentheses as follows:

### Coercion of Values Returned from Arithmetic Operators

When evaluating an expression that includes an arithmetic operator, the type of value obtained depends upon a combination of the operator and the types of the values obtained from the operand terms. The following table summarizes this behavior:

### Constraints on Exponentiation Operations

G2 disallows certain values to participate in an exponentiation operation, as follows:

## Using Logical Operators

Logical operators specify boolean operations on either one or two operand terms for which G2 obtains values of type `truth-value`, as summarized in this table:

### Short-Circuited (Lazy) Evaluation of Logical Operators

G2's `and` and `or` operators are short-circuited:

Short-circuited (also called lazy) evaluation avoids wasting processor time evaluating terms in a logical expression whose value is already known.

### Affecting the Expiration Time

For an expression that includes a logical operator and that refers to one or more logical variables, the expiration time of the entire expression depends on which logical operator is used, as follows:

Tip: Logical operators can also combine with terms that produce fuzzy truth values. For more information, see the section
Producing Fuzzy Truth Values From Relational Operations.

### Precedence and Order of Evaluation

The precedence for logical operators is as follows:

An example is:

Because the `not` operator has higher precedence than `and`, and because the `and` operator has higher precedence than the `or` operator, G2 associates the operators and operands as follows:

By default, logical operators have lower precedence than arithmetic and relational operators. So, G2 evaluates these two expressions in the same manner:

## Using Relational Operators

A relational operator causes G2 to compare the values obtained for its two operands and to return a value of type `truth-value`.

Tip: There are corresponding relational operators for fuzzy truth expressions. See the section Fuzzy Truth Operators for more information.

## Producing Fuzzy Truth Values From Relational Operations

A truth-value expression that includes a relational operator produces a value of type `truth-value`. As introduced in Using the Truth-Value Type, a value of type `truth-value` can range from `-1.0 true` to `+1.0 true`, where `-1.0 true` signifies complete certainty that a comparison is false and `+1.0 true` signifies complete certainty that a comparison is true. Thus, such an expression can also produce a fuzzy truth value, which signifies a partial certainty in the truth of a comparison.

### Specifying a Fuzzy Truth Band Subexpression

To produce a fuzzy truth value from an expression that includes a relational operator, specify a fuzzy truth band subexpression. Its syntax is:

For example, this truth-value expression compares an attribute's value with a literal integer, according to a fuzzy truth band subexpression:

The fuzzy truth band subexpression `( +- 5 )` specifies that the fuzzy truth value produced corresponds to a range of values, from 45 (50 minus 5) to 55 (50 plus 5). That is, as the value of the `Flow` attribute of `pump-1` ranges from 45 to 55, the corresponding fuzzy truth values range from `-1.0 true` to `+1.0 true`.

If the `Temperature` attribute of `tank-1` has a value of 101.8, the subexpression causes the entire truth-value expression to produce the fuzzy truth value of `+0.9 true`. Because the value `+0.9 true` is greater than the value of `Truth-threshold` (that is, `+0.8 true`), G2 executes the action to conclude the truth-value (`+0.9 true`) into the `Tank-is-boiling` attribute of `tank-1`.

### Using Logical Operators with Terms That Produce Fuzzy Truth Values

You can use logical operators to combine terms in a truth-value expression that produce fuzzy truth values. For example, you can specify a truth-value expression like this:

G2 evaluates these expressions as follows:

Given this truth-value expression that includes the `and` logical operator:

G2 evaluates this expression as follows:

Given this truth-value expression that includes the `or` logical operator:

G2 evaluates this expression as follows:

Given this truth-value expression that includes the `not` logical operator:

G2 evaluates this expression as follows: If the subexpression `X > Y ( +- .2 )` evaluates to a fuzzy truth value of `+0.3 true`, G2 produces the value `-0.3 true` for the entire truth-value expression.

### Fuzzy Truth Operators

You can specify a truth-value expression that compares one fuzzy truth value to another. This kind of expression uses one of these fuzzy truth operators:

A truth-value expression that specifies a fuzzy truth operator produces a truth-value of either `true` or `false`. It does not produce a fuzzy truth value.

In an expression that specifies a fuzzy truth operator, if either truth-value expression term being compared is more complex than a reference to a logical variable or logical parameter, enclose that term in parentheses, such as:

## Using the Concatenation Operator

G2 provides a concatenation operator that you specify as the pair of characters [ and ]. Use this operator to insert, prepend, or append a `text` version of any value to a literal `text` value.

In this case, G2 converts the value produced by the expression `the public-name of the water-pipe connected to tank-1` to a text value and inserts it into the literal text enclosed in double quotes. Likewise, G2 converts the value produced by the expression `the direction-of-flow of the water-pipe connected to tank-1`.

To concatenate by appending, specify an expression such as:

You can also use the concatenation operator to produce an entire `text` value, as in this `change the text of` action:

G2 can convert to a `text` value any expression specified within the concatenation operator characters.

### Formatting Using the Newline Character

You can include a newline character in a literal `text` value, by pressing Control + j within the two double quotes (") characters that enclose the value. However, including a newline character within the [ and ] concatenation operators only formats that line of code; G2 does not include the newline character in the literal `text` value.

For example, this literal text value does not include a newline character, because its author specified the newline character between the [ and ] operators:

### Formatting Numeric Values

Within the concatenation operator, you can optionally specify how G2 formats a numeric value: its number of decimal digits left and/or right of a decimal point, as a time-stamp, or as a time interval.

Here is the syntax for a formatting expression:

The formatting alternatives in this expression are also those for formatting the display of a numeric value in a readout table. See Readout Tables.

A time value is either an integer or float value. You can capture a time value by using one of the G2 time functions or one of these expressions:

The next figure shows a workspace whose readout tables demonstrate how the `as time stamp` and `as interval` formatting expressions affect differently the display of the current time and a literal float value.