# Xbasic GuideExpressions

An expression is a combination of variables, constants, operators, and functions that evaluate to another value. For example, in the code below, the text to the right of the equal (=) sign is an expression:

`Name = Firstname + " " + Lastname`

### Operators

An operator is a symbol used to represent a mathematical, relational, logical, or string operation. For example, the asterisk (*) character represents multiplication. Functions, field names, and constants can be combined with operators to form complex expressions. Most operators use the following syntax:

`expression operator expression`

We have used several operators and functions in this guide already, including the assignment (=) operator to assign values to variables, the concatenation (+) operator to create character strings by combining variables (name, dayOfWeek) and string constants ("Hello ", "Today is "), and the ? output operator to display the values of variables and functions in the Interactive Window.

```result = 12 * 7
? result
= 84```

Expressions execute in the order of Precedence. Operator Precedence defines the order in which operators in an expression execute. In general, operators are evaluated in the following order:

• Parentheses and Function calls
• Negation (.NOT.)
• Exponentiation (^, **)
• Multiplication, Division, and Substring Inclusion (*, /, \$, !\$)
• Addition and subtraction (+, -)
• Relative and relative or equal (<=, >=, <, >)
• Equal, Exactly Equal, Not Equal, and Not Exactly Equal (=, ==, <>, !=)
• And and Exclusive Or (.AND., .XOR.)
• OR (.OR.)

Including operators in order according to their precedence allows implied parentheses when combining operations. For example:

```A >= 5 .AND. B = 10 .OR. A >= 50
(A >= 5 .AND. B = 10) .OR. A >= 50```

Because the .AND. operator has precedence over the .OR. operator, both statements are equivalent. When multiple operations have the same precedence, they evaluate from left to right. For example:

```? 0.6 < 10 .AND. "Apple"="Apple" .AND. "Alpha" < "Beta"
= .T```
• ### Mathematical Operators

Mathematical operators are used in numeric, date, or character expressions to yield results of the same type.

• Operator

Description

Example

()

Parentheses. Used to group operations

```? (3 + 2) * 5
= 25```

^

Exponentiation.

```? 10^2
= 100```

**

Exponentiation.

```? 7**2
= 49```

*

Multiplication.

```? 8 * 8
= 64```

/

Division.

```? 144 / 12
= 12```

+

Addition. Adds two numbers, dates, or time values together. When used with characters, the two variables are concatenated.

```? 1+3
= 4
? {1/23/2001}+30
= {02/22/2001}
? {08/19/2019 03:14:58 77 pm} + 32000
= 08/20/2019 12:08:18 77 am
? "Welcome" + " " + "Home"
= "Welcome Home"```

-

Subtraction. Subtracts a number from another number, days from a date, or seconds from a time or date-time value. When used with character strings, concatenates two character values together, removing trailing whitespace from the first character string.

```? 12 - 7
= 5
? {3/15/2001} - 90
= {12/15/2000}
? {03:14:58 77 pm} - 3600
= 02:14:58 77 pm
? "race " - "car"
= "racecar"```
• ### Comparison Operators

Comparison operators compare two expressions which must be of the same type (either character, numeric, or date) and returns a result of true (.T.) or false (.F.). Any expression involving a comparison operator is called a logical expression. Comparison operators are typically used in creating search criteria, filters, or are used with the IF and CASE functions. All comparison operators, except for substring inclusion, can evaluate numeric, date, or character values. For date values, earlier dates have lower values. For character values, the character's corresponding ASCII value is used in the comparison.

• Operator

Description

Example

=

Equals. When used in a logical expression, compares two values for equality. If used with character strings, removes all trailing whitespace and performs a case-insensitive comparison.

```? 7 = "7"
= .F.
? 7 = 4+3
= .T.
? "hello " = "HELLO"
= .T.```

==

Exactly Matching. When used in a logical expression, compares two values for equality. When used with character strings, performs a case-sensitive comparison. Does not remove whitespace.

```? 7 == "7"
= .F.
? 7 == 4+3
= .T.
? "hello " == "HELLO"
= .F.```

>

Greater Than.

```? 7 > 7
= .F.
? {01/01/2020} > now()
= .T.```

>=

Greater Than or Equal To.

```? 7 >= 7
= .T.
? {01/01/2020} >= now()
= .F.```

<

Less Than.

```? {01/01/2020} < now() = .F.
? 10 < 12
= .T.```

<=

Less Than or Equal To.

```? 7 <= 6 = .F.
? {06/12/2019} <= date()
= .T.```

<>

Not Equal. When used with character strings, the <> operator removes trailing whitespace from both strings and performs a case-insensitive comparison.

```? 2 <> 3 = .T.
? "Foley " <> "foley "
= .F.
? "Foley" <> "folley"
= .T.```

!=

Not Exactly Matching. When used with character strings, performs a case-sensitive comparison. Does not remove whitespace.

```? 2 != 3
= .T.
? "Foley " != "foley "
= .T.
? "Foley" != "folley"
= .T.```

\$

Contains Substring. Performs a case-insensitive test to determine if the first character string is found in the second character string.

```? "CARS" \$ "racecars"
= .T.
? "cars" \$ "racecars"
= .T.```

\$\$

Contains Substring. Performs a case-sensitive test to determine if the first character string is found in the second character string.

```? "CARS" \$\$ "racecars"
= .F.
? "cars" \$\$ "racecars"
= .T.```

!\$

Does Not Contain Substring. Performs a case-insensitive test to determine if the first character string does not exist in the second character string.

```? "cats" !\$ "racecars"
= .T.```
• ### Logical Operations

Logical operators are used between logical expressions (two expressions that return a .T. or .F. value) to yield logical results.

• Operator

Description

Example

=

Equals.

```? .T. = .T.
= .T.
? .F. = .F.
= .T.
? .F. = .T.
= .F.```

.AND.

Logical AND.

```? .T. .AND. .F.
= .F.```

.OR.

Logical OR.

```? .T. .OR. .F.
= .T.```

.XOR.

Logical XOR.

```? .T. .XOR. .F.
= .T.
? .T. .XOR. .T.
= .F.
? .F. .XOR. .F.
= .F.```

.NOT.

Negation.

```? .NOT. (4 == 5)
= .T.```

### Delimiters

Constant values can be assigned to character strings using one of two methods: double-quotes (") and delimiters. When declaring multi-line character strings with double-quotes, you must concatenate additional lines using a combination of the crlf() function to insert a new line and the concatenation (+) operator. For example:

```DIM str AS C
str = "This is the first line." + crlf()
str = str + "This is the second line." + crlf()
str = str + "This is the last line."```

This example can be simplified by using delimiters. Delimiters are used to define multi-line character strings. A character string declared using delimiters begins with the <<%delimiter% statement and ends with the %delimiter% statement. The text, delimiter, can be any value you desire as long as it is identical in the beginning and ending delimiter operator. For example, we can recreate the previous example using delimiters as follows:

```DIM str AS C
str = <<%myStr%
This is the first line.
This is the second line.
This is the last line.
%myStr%```

IMPORTANT: <<%DELIMITER% must be terminated with a newline. Any spaces or other characters after the closing % will result in an "Extra characters at end of expression" error.

The Xbasic auto help system provides suggestions for delimiters as you write your scripts. There are several special case delimiters, such as %code% and %html%, which add unique behaviors such as syntax highlighting and code validation. The delimiters offered through auto help include:

Delimiter

Description

<<%code%

%code%

Xbasic code string. Xbasic written inside code blocks will have full access to auto help and includes syntax highlighting.

<<%css%

%css%

CSS code string. CSS written inside a CSS block will include syntax highlighting and auto help.

<<%html%

%html%

HTML code string. HTML written inside html blocks will have syntax highlighting and auto help.

<<%js%

%js%

JavaScript code string.

<<%json%

%json%

JSON string.

<<%xml%

%xml%

XML string. XML code written inside xml blocks will have syntax highlighting and auto help.

<<%str%

%str%

<<%txt%

%txt%

Text string. No special formatting is applied.

`'This is a comment`
```'Square the value
```'DIM field_value AS D