Introduction to Expressions

An expression is a syntactically valid series of operands and operations that specifies how to compute a value. In some contexts the null expression is allowed and in others it is not. A null expression is allowed in the "for" statement and the expression statement (giving the null statement consisting of only ';'). In most other places a null expression will give a compilation error.

Except as indicated by the syntax of precedence of the operators or otherwise specified below (for the function call operator (), &&, ||, ?:, and comma ','), the order of evaluation of an expression is unspecified. An expression involving more than one occurrence of commutative and associative binary operators at the same level may be rearranged arbitrarily by the compiler, even in the presence of parenthesis, provided the type of the operands or of the results are not changed thereby. To force a particular grouping of operations, an explicit temporary or cast must be used. In other words:

       x=(x*y)*(w*v);

may not multiple in the order implied by the parenthesis at all.

Evaluation of an expression (especially one involving functions, incrementing, decrementing, or assignment) may produce 'side effects', which are changes in state of the target environment incidental to the evaluation. The order of evaluation of sub-expression and hence of side effects within one expression evaluation is unspecified. If an expression has no side effect, the compiler may choose not to evaluate it - in which case it will usually give a warning.

An 'lvalue' is an expression that refers to the location of an object. Some operators yield lvalues. The discussion below of each operator will indicate whether it expects or yields lvalues.

The word 'lvalue' comes from the assignment expression:

    E1 = E2

whose meaning is: the expression on the left, E1, will be or must imply an address of where to put the value of the expression on the right, E2. The same expression on the left, E1, if appearing on the right would mean the value stored at the lvalue address.

Thus in the statement:

    x=y+5;

The lvalue expression is, 'x'; and the rvalue expressions are: 'y', '5', and 'y+5'. While in the following statement:

   x=5+y/x;

Again the lvalue expression is 'x', but 'x' is also a sub expression and an rvalue in the total rvalue expression: 5+y/x.

The simplest valid expressions consist of either just a:

All other expressions are built using combinations of these three and the syntactically legal sequences with operators and functions. The operators and any special features of any of them are described in detail below.


© 1991-2008 Prem Sobel. All Rights Reserved.