EXPRESSION, STATEMENTS AND SIMPLE I/O

CONTENTS:

  1. Expressions.
  2. Operators.
  3. Statements.
  4. Type equivalence.
  5. Coercion, casting and conversion.
  6. Standard output operators.
  7. Standard input operators.



1. EXPRESSIONS


Boolean expressions

Boolean expression return the values true or false. Well know Boolean operators include:

CAdaPascal
Equals == = =
Not equals != /= <>
Less than < < <
Greater than > > >
Less than or equal to <= <= <=
Greater than or equal to >= >= >=
Logical and && and and
Logical or || or or
Logical not ! not not

The first 6 of the above are sometimes referred to as comparison 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.



2. OPERATORS


Ternary Operators

Some languages support ternary operators (arrity = 3). For example the "ternary condition operator" (?:) supported by C:

<condition> ? <expression1> : <expression2>

Returns expression1 if condition is true and expression2 otherwise. Example:

#include <stdio.h>

main () {
    int number;

    scanf("%d",&number);

    printf("The number %d is an ",number);
    (number/2)*2 == number ? printf("even") : printf("odd");
    printf(" number.\n");
    }
#include < stdio.h>

int division(int, int);

void main(void) {
    printf("Result of ternary operation = %d\(bsn",division(0,2));
    printf("Result of ternary operation = %d\(bsn",division(8,2));
    printf("Result of ternary operation = %d\(bsn",division(2,1));
    printf("Result of ternary operation = %d\(bsn",division(1,0));
    }

int division(int num1, int num2) {
    return(num2 != 0 ? num1/num2 : 0);
    }

2.1. PRECEDENCE OF OPERATORS

LevelOperator
2+, - (unary plus and minus)
3Exponential
4*, /, mod, div
5+, -
6Comparison operators
7Logical not
8Other logical operators
9Assignment
#include < stdio.h>

void main(void) {
    int numA = 2;
    int numB = 4;

    printf("%d\(bsn",numA+numB*numB);
    printf("%d\(bsn",numB*numB+numA);
    printf("%d\(bsn",(numA+numB)*numB);
    printf("%d\(bsn",numA*2==numB);
    printf("%d\(bsn",numA+2==numB*2);
    }

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.


The and Operator and Lazy Evaluation

Consider the following C expression:

(quantity != 4) && (number_x == quantity+value*4) && (number >= 0)

This sequence will be evaluated from left to right (&& is left associative) until the end of the sequence is reached or one of the operands for the and operators fails. This process of not needlessly evaluating each operand is termed lazy evaluation. Lazy evaluation is an important concept in functional languages.

The above is not supported by Ada. In the case of the Ada and operator no order of evaluation (associativity) is specifyed, i.e. all operands for a sequence of and operators will be evaluated regardless of whether this is necessary or not. The effect of "lazy evaluation", as illustrated in the above C example, can be achieved in Ada by writing:

(QUANTITY /= 4) and (NUMBER = QUANTITY+VALUE*4) then (NUMBER >= 0)

C example:

#include < stdio.h>

void main(void) {
    int numA = 2;
    int numB = 4;

    printf("%d\(bsn",numA+numB*numB);
    printf("%d\(bsn",numB*numB+numA);
    printf("%d\(bsn",(numA+numB)*numB);
    printf("%d\(bsn",numA*2==numB);
    printf("%d\(bsn",numA+2==numB*2);
    }

2.2 HE C POST-INCREMENT AND PRE-INCREMENT OPERATORS

Certain assignment expressions such as:

j = j+1;            J := J+1;
k = k-1;            K := K-1

are so common that some imperative languages (but not Ada) provide a short hand notation for them:

i++;				i--;

The operators are referred to as the post-increment and pre-increment operators.




3. 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, Pascal and C this is a semi-colon symbol (;).


3.1. Assignment Statements


3.2. Assignment Expressions

Assignment expressions are generally of the form:

<destination> <operator> <something>

Ada and C example:

NUMBER := INDEX*2;                                      number = index*2
VALUE := VALUE+1                                        value = value+1
COUNTER := COUNTER-1                                    counter = counter-1

The last two pairs of examples are so common that some imperative languages (C but not Ada) provide a short hand notation for them:

value++                                                 counter--

The ++ and -- operators used here are sometimes referred to as the post-increment and pre-increment operators.




4. TYPE EQUIVALENCE




5. COERCION, CASTING AND CONVERSION

5.1. Coercion

5.2. Casting

5.3. Conversion




6. OUTPUT OPERATIONS

Outputting a given value comprises two operations:

  1. Converting the value to a string
  2. Displaying the string

In most imperative languages both operations can best be achieved using specially provided built in library routines. Examples:

Adaput(NUMBER);
Cprintf("%d",number);
Pascalwrite(NUMBER);

Note that in C the user has to carry out the necessary conversion. This is not necessary in Ada. The put statement will work with data of any primitive type. A feature known as polymorphism. Polymorphism is an important concept in modern functional languages such as SML, Haskell and Miranda. Note also that in Ada, by default, numbers are "right justified" in a "field" big enough to hold the largest number for the type - this can be avoided by including a field width indicator (see example given below).




6. INPUT OPERATIONS

When inputting a value its type must be checked dynamically.


C Example

#include <stdio.h>

void main(void)
{
int number;

scanf("%d",&number);
printf("number = %d\n",number);
}

Note the use of an "ampersand" (&) indicating that we are putting something into the address of number.

Ada Example

with CS_IO ; use CS_IO ;

procedure INPUT_EXAMPLE is
        NUMBER: integer;
begin
        get(NUMBER);
        put("NUMBER = ");
        put(NUMBER, width => NUMBER);
        new_line;
end INPUT_EXAMPLE ;

Note that in Ada (also Pascal) there is no need to specify the input type as in the case of C.




Return to imperative home page or continue.

Created and maintained by Frans Coenen. Last updated 03 July 2001