EXPRESSIONS AND STATEMENTS


1. EXPRESSIONS


2. BOOLEAN EXPRESSIONS

Expressions that use Boolean operators return the values TRUE or FALSE. Well know Boolean operators include:

Ada encoding
Equals =
Not equals /=
Less than <
Greater than >
Less than or equal to <=
Greater than or equal to >=
Logical and and
Logical or or
Logical not not

The first 6 of the above are sometimes referred to as comparison or relational operators and the remaining 3 as logical operators. Examples of Boolean expressions:

A_VALUE < 50

B_VALUE = 0

X_VALUE >= 10 and X_VALUE <= 20

X_VALUE < 10 or X_VALUE > 20

A_VALUE < 20.0 and (B_VALUE /= 'q' or testFun(A_VALUE, C_VALUE) = 1)

These can be interpreted as follows:

  1. Return TRUE if value for data item A_VALUE is less than 50, otherwise return FALSE.
  2. Return TRUE if value for data item B_VALUE is equal to 0, otherwise return FALSE.
  3. Return TRUE if value for data item X_VALUE is greater than or equal to 10 and less than or equal to 20 (i.e. X_VALUE is within the range 10..20), otherwise return FALSE.
  4. Return TRUE if value for data item X_VALUE is less than 10 or greater than 20 (i.e. X_VALUE not within the range 10..20), otherwise return FALSE.
  5. Return TRUE if value for data item A_VALUE is less than 20.0 and either the value for B_VALUE is the character 'q' or the value returned by the test function (which has two arguments A_VALUE and C_VALUE) is equal to 1, otherwise return FALSE.

3. OPERATORS

3.1. Precedence of operators

LevelOperator
2+, - (unary plus and minus)
3** (exponential)
4*, /, rem, mod
5+, -
6Comparison operators
7Logical not
8Other logical operators
9:= (assignment)

The order of execution of operators on the same level is controlled by associativity:

If an operator is non-associative it cannot be used in a sequence.

Note on rem and mod operators.


4. STATEMENTS

Statements are the commands in a language that perform actions and change the "state" (the term is used for historical reasons). The state of a program is described by the values held by its variables at a specific point during its execution. Example statements:

Statements are usually separated by some operator (symbol), in Ada this is the semi-colon character (;).


5. ASSIGNMENT STATEMENTS


5.1. Type equivalence

Thus, in Ada if we wish to use two operands each of a different type in an expression we must convert one of the operands so that its type is equivalent to that of the other. Remember Ada type conversions have the following form:

T(N)

6. PROGRAM CONSTRUCTS

Program constructs are program statements which are used to control the order (flow) in which statements are executed (or not executed). There are a number of recognised basic program constructs that can be classified as follows:

To which we can also add routine invocation. Note also that each of the above constructs are all also program statements in their own right.


6. SEQUENCES

A sequence construct tells the processor which statement is to be executed next. By default, this is the statement following the current statement (or the first statement in the program). In the early days of computing, if we wished to "jump" to some statement other than the following statement a "goto" construct was used. This is still supported by Ada, however, its usage is considered to be bad practice as it makes it difficult to "trace" a program's execution and to determine the state of variables at a given point during processing. As a result errors are often obscure and difficult to locate. For this reason "goto" statements should be avoided. In most cases we can use a procedure or function call (i.e. routine invocation) instead.




Created and maintained by Frans Coenen. Last updated 11 October 1999