Expressions

Expressions are the building blocks of complex behavior in any programming language.  They are what allow you to combine simple terms to perform meaningful operations, and eventually output a single ‘value’.

Summary
Expressions are the building blocks of complex behavior in any programming language.
Each expression is composed of one or more terms, perhaps joined together by operators or procedure calls.
Operators are what connect various terms of an expression and supply the meaning of each.
Arithmetic operators provide the basic mathematical operations on numbers.
Bitwise operators use integers and operate on the bits that represent them.
Comparisons do just that, compare.
Logical operators perform boolean logic on the values.
Assignment operators are used to put a value into something, be it a variable or database.
Precedence determines the order of evaluation for all operators.

Expression Basics

Each expression is composed of one or more terms, perhaps joined together by operators or procedure calls.  Almost any place that calls for a value, you can use an expression to supply that value.  Combine constants, variables, function calls, database lookups, search results, anything.  As long as the types are consistent (or compatible) you’ve got a valid expression.

Operators

Operators are what connect various terms of an expression and supply the meaning of each.  They compare or subtract, whatever needs to happen.  They fall into one of several categories depending on their function.  Let’s look at a breakdown of each of these categories.

Arithmetic Operators

Arithmetic operators provide the basic mathematical operations on numbers.  If you’re looking to add or subtract, it’ll be in this group.

+    Integer addition
- Integer subtraction
/ Integer division
% Integer modulus (remainder)
* Integer multiplication

Bitwise Operators

Bitwise operators use integers and operate on the bits that represent them.  The result of bitwise operations is an integer (whose bits are the result of the operation).

|    Bitwise OR
^ Bitwise XOR
& Bitwise AND
~ Unary bitwise negation
<< Bitshift left
>> Bitshift right

Comparison Operators

Comparisons do just that, compare.  For integers, this is a numerical comparison, and for strings, this is an alphabetical comparison (as in an alphabetization (A-Z) of ascii values).  Comparison operations return a boolean value (0 for false, 1 for true) in an integer.

=    Equality
!= Inequality
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
~= Wildcard comparison (strings only)

Logical Operators

Logical operators perform boolean logic on the values.  They take boolean values on each side (or if normal values are used, the following rules are used to evaluate the truth value of a term: 1) if it’s an integer, non-zero is true, 0 is false, 2) if it’s a string, a length greater than 0 is true, otherwise false, 3) if it’s a vector, a size greater than 0 is true, otherwise false).

or    Logical OR
and Logical AND
! Logical negation (non-zero -> 0 or 0 -> 1)

Assignment Operators

Assignment operators are used to put a value into something, be it a variable or database.  The assignment operator is the ‘=’ character.  However, it can be combined with various other operators to provide a shortcut in writing an assignment.  For example: ‘+’ can be combined with ‘=’ to form ‘+=’.  When using an operator in this way, the assignment “x += 5” is treated like “x = x + 5” for all practical purposes.

The operators ‘++’ and ‘--’ can be used to add or substract a 1.  Using ‘a++’ is identical to ‘a += 1’, and ‘a--’ corresponds to ‘a -= 1’.

Operator Precedence

Precedence determines the order of evaluation for all operators.  Lower precedence operations are performed after higher precedence ones.  The following listing provides a listing of the precedence for all operators in increasing precedence.

*Operator*          *Description*
= Assignment
?: Conditional operator (condition ? val1 : val2)
or Logical OR
and Logical AND
| Bitwise OR
^ Bitwise XOR
& Bitwise AND
=,!=,<,>,<=,>=,~= Comparisons
<<,>> Bitwise shift
+,- Addition and subtraction
*,/,% Multiplication, division, modulus
!,~,- Unary operations (logical, bitwise,
and mathematical negation)
.,(),[] 'Dot' connector, parentheses, indexing