Coronavirus (Covid-19): Latest updates and information
Skip to main content Skip to navigation

Procedural statements

Procedural Statements are executable statements. The statements are executed in sequence except as indicated.

Expression Statement

Most statements are expression statements, which have the form:

expression ;

Usually expression statements are assignments or function/procedure calls. For instance:

I = 1;                /* assignment statement*/
J++;                  /* another form of assignment */
DoSomething(I, J);    /* procedure call */

Insert Statement

The statement

insert lvalue , expression-1 , expression-2 ;

inserts a value evaluated from expression-2 into the list object referred to by lvalue at the position expression-1; expression-1 must be of integral type and cannot be smaller than 1 or be greater than the current number of items of the list plus one. The list object referred to by lvalue must be a read/write variable.

Append Statement

The statement

append lvalue , expression ;

appends a value evaluated from expression to the end of the list referred to by lvalue. It is equivalent to do

insert lvalue , lvalue # + 1 , expression ;

but the lvalue expression is evaluated only once.

Delete Statement

The statement

delete lvalue , expression ;

deletes a value from the list object referred to by lvalue at the position evaluated from expression; expression must be of integral type and cannot be smaller than 1 or be greater than the current number of items of the list; whence the list cannot be a null list. The list object referred to by lvalue must be a read/write variable.

Shift Statement

The syntax of the shift statement is:

shift lvalueopt ;

The shift statement deletes the first value from the list referred to by lvalue; whence the list cannot be a null list. If lvalue is omitted, the argument list $ is assumed; hence it can only be used within a function body. The equivalent delete statements of the shift statements are:

delete $ , 1 ;     /* shift ; */
delete lvalue , 1 ;        /* shift lvalue ; */

Compound Statement

So that several statements can be used where one is expected, the compound statement is provided:

compound-statement:
        { statement-listopt }

statement-list:
        statement
        statement  statement-list

Condition Statement

The two forms of the conditional statement are:

if ( expression ) statement
if ( expression ) statement else statement

In both cases the expression is evaluated and if it is true, the first sub-statement is executed. In the second case the second sub-statement is executed if the expression is false or @. As usual the ``else'' ambiguity is resolved by connecting an else with the last encountered else-less if.

While Statement

The while statement has the form

while ( expression ) statement

The sub-statement is executed repeatedly so long as the value of the expression remains true. The test takes place before each execution of the statement.

Do Statement

The do statement has the form

do statement while ( expression ) ;

The sub-statement is executed repeatedly until the value of the expression becomes false or @. The test takes place after each execution of the statement.

For Statement

The for statement has the form

for ( expression-1opt ; expression-2opt ; expression-3opt ) statement

This statement is equivalent to

expression-1 ;
while ( expression-2 ) {
        statement
        expression-3 ;
}

Thus the first expression specifies initialization for the loop; the second specifies a test, made before each iteration, such that the loop is exited when the expression becomes false or @; the third expression often specifies an incrementation which is performed after each iteration.

Any or all of the expressions may be dropped. A missing expression-2 makes the implied while clause equivalent to

while (1);

other missing expressions are simply dropped from the expansion above.

Switch Statement

The switch statement causes control to be transferred to one of several statements depending on the value of an expression. It has the form

switch ( expression ) statement

The statement is typically compound. Any statement within the statement may be labelled with one or more case prefixes as follows:

case constant : statement

There may also be prefix of the form:

default : statement

When the switch statement is executed, its expression is evaluated and compared with each case constant. If one of the case constants is equal to the value of the expression, control is passed to the statement following the matched case prefix. If no case constant matches the expression, and there is a default prefix, control passes to the prefixed statement. If no case matches and if there is no default then none of the statements in the switch is executed.

Break Statement

The statement

break ;

causes termination of the smallest enclosing while, do, for, or switch statement; control passes to the statement following the terminated statement.

Continue Statement

The statement

continue ;

causes control to pass to the loop-continuation portion of the smallest enclosing while, do, or for statement; that is to the end of the loop.

Return Statement

A function returns to its caller by means of the return statement, which has one of the forms:

return expressionopt ;

In the optional expression is omitted the value returned is @. Otherwise, the value of the expression is returned to the caller of the function. Flowing off the end of a function is equivalent to return @.

Null Statement

The null statement has the form:

;

A null statement is useful to carry a label just before the } of a compound statement, for example:

switch (...) { ... case 1: ; }

or to supply a null body to a looping statement such as while, for example:

while (1) ; /* an infinite waiting loop */