. navigate
 Navigate
5. Expressions left arrow
x
right arrow 7. Procedures, Functions and Parameters
Red Reference Manual
6.

STATEMENTS

Assignment     Begin     If     Case     Repeat     Exit     Return     Goto




6.   STATEMENTS

statement diagram
C - identifier   38 - simple statement   39 - compound statement


statement diagram
24 - allocation   40 - assignment   45 - exit   46 - return   47 - goto   49 - proc invocation   59 - raise   60 - reraise   62 - task invocation


compound statement diagram
41 - begin   42 - if   43 - case   44 - repeat   58 - guard   63 - wait   65 - region

Statements fall into two categories: compound and simple. A compound statement contains a header, one or more bodies (with delimiters between bodies), and an ending. Any compound statement can be prefixed and suffixed by matching identifiers. These identifiers can serve as the target of an exit statement and, in addition, enhance program readability. Each compound statement is an open scope. The only definitions which are local to a compound statement are the matching identifiers and, in the case of a repeat statement, the index variable. Notice, however, that each body contained in a compound statement is a scope that may contain local definitions.

A simple statement does not contain a body, cannot be surrounded by matching identifiers, and is not a scope. Any statement may optionally be preceded by labels that can serve as the target for a goto statement.



RULES

Identifier 1 is called a goto label and is defined in the scope immediately containing the statement it prefixes.

Identifiers 2 and 3, called matching identifiers, must be identical. The matching identifier is defined in the scope of the compound statement which it brackets.



NOTES

     Goto labels and matching identifiers are automatically inherited by open scopes (see Section 3.5), but are never inherited by closed scopes, and may never be exported from a capsule (see Section 8.2). A matching label can never be the target of a goto statement, and a goto label can never be the target of an exit statement.

Even though there is no "empty statement", empty bodies are permitted (see Section 3.2).


RED RATIONALE

RED, like Pascal, is a "statement" language as opposed to an "expression" language like Algol 68.



6.1  ASSIGNMENT STATEMENT

assignment statement diagram
26 - expression   28 - variable

The assignment statement is used to copy the value of an expression into a variable.



RULES

The expression must have the same type as the variable; that type must be assignable (see Section 4.3).

Elaboration of the assignment statement replaces the current value of the variable with the value of the expression.

The value of the expression must satisfy any subtype constraints of the variable (e.g., range, precision, array bounds); otherwise, the appropriate exception is raised.



NOTES

    Entire arrays, array slices, and records may be assigned in a single assignment.

    For rules concerning the assignment of built-in types, see Appendix C. For rules concerning the assignment of user-defined types, see Section 13.2.



EXAMPLES

assignment examples



6.2  BEGIN STATEMENT

begin statement diagram
2 - body

The begin statement can be used to group together a sequence of related statements. Since the begin statement introduces a new open scope, it is also a convenient means for localizing declarations to the sequence of statements where they are needed.



RULES

The begin statement is elaborated by elaborating its body.



NOTES

    All compound declarations and compound statements contain bodies. It is therefore unnecessary to write a begin statement in those contexts in order to achieve grouping of multiple statements into a single statement, or to achieve a new scope for declarations.



EXAMPLES

begin examples




6.3  IF STATEMENT

if statement diagram
2 - body   26 - expression

The if statement selects one of several alternative bodies for elaboration, based on the value of one or more boolean expressions.



RULES

Each expression must have type BOOL.

Elaboration of an if statement proceeds by elaboration of each expression in order until either an expression with value true, or a final ELSE is reached; then the corresponding body is elaborated. If no expression has value true, and no final ELSE is present, none of the bodies is elaborated.


RED RATIONALE

RED generalizes Pascal's IF statement by replacing the single statement format with bodies that may contain declarations. The presence of the ELSEIF in RED's if statement becomes useful for avoiding cascaded END IFs.



EXAMPLES

if examples



6.4  CASE STATEMENT

case statement diagram
2 - body   14 - range   26 - expression

The case statement allows a body to be selected for elaboration from one or more alternate bodies, based on the value of a single expression.



RULES

Between each WHEN and => is a list of value labels. A value label can be an expression (a single value label), or a range (a range value label). The expressions in all value labels, and the expression following CASE must be of the same type. If only single value labels appear, the type must be one for which = is defined. If any range labels appear, the type must be one for which + and < are defined.

Elaboration of a case statement consists of the following steps:

  1. The value of the expression following CASE is compared to the values of the value labels. A match is found if the value of the CASE expression is either equal to the value of a single value label or is within the range of a range value label (see Section 4.1.7). The order in which value labels are examined is undefined.

  2. If one or more matches are present, then the body associated with one of the matching value labels is elaborated.

  3. If no match is found, and the ELSE is present, the body following ELSE is elaborated.

  4. If no match is found, and the ELSE is not present, the X_CASE exception is raised.



NOTES

    When more than one matching value label is found, it cannot be predicted which of the bodies associated with the matching labels will be elaborated.

    Use of the case statement for user-defined types is described in Section 13.6.


RED RATIONALE

RED's case statement is more reliable and less error prone than the Pascal version. Because alternatives can contain declarations as well as statements, RED provides better support for top down programming. The allowance of nonmanifest expressions in RED enables values from programmer-defined types to be used as case labels without complicating the rules for manifest computation.

The generality of the CASE statement in RED is paid for only when it is used.



EXAMPLES

case examples



6.5  REPEAT STATEMENT

repeat statement diagram
C - identifier   2 - body   12 - subtype   26 - expression

The repeat statement allows a body to be elaborated zero or more times. The for phrase has an index that takes on successive values of the specified subtype. The while phrase is used to achieve conditional repetition.



RULES

For Phrase

The identifier, called the index is defined as a variable with the specified subtype in the scope of the repeat statement. The index is treated as a readonly data item within the body (see Section 4.2).

Elaboration of a repeat statement with for phrase proceeds as follows. The index is created before the first repetition. During the first repetition, the index has the lowest value of the subtype (i.e., .MIN). For each subsequent repetition, the index will have the successive value of the subtype (via SUCC) until the last value (.MAX) is reached. If REVERSE is specified, the index has the value .MAX for the first repetition, and has successive values (via PRED) until the last value (.MIN) is reached.


RED RATIONALE

The syntax chosen for the FOR clause emphasizes the fact that the loop control variable is locally declared at that point. Since the loop control variable is readonly within the body of the repeat, and since it is inaccessible outside the body, it is simple for the implementation to optimize by preserving the variable in a register during loop elaboration.


While Phrase

The expression must have type BOOL. Elaboration of the repeat statement with a while phrase repeatedly elaborates the body while this expression is true. The value of the expression is tested prior to each elaboration of the body.



NOTES

    The index is local to the repeat statement; i.e., neither its definition nor its value is directly available outside the repeat statement. If the subtype of the index has no values, the body is not elaborated.

Additional termination conditions can be inserted anywhere within the body of a repeat statement by the conditional use of an exit statement (see Section 6.5).

Use of the repeat statement for user-defined types is described in Section 13.7.



EXAMPLES

repeat example



6.6  EXIT STATEMENT

exit statement diagram
C - identifier

The exit statement terminates the elaboration of an enclosing compound statement.



RULES

The identifier must be known as a matching identifier.

Elaboration of the exit statement causes the elaboration of the compound statement bracketed by the matching identifier to be terminated.


RED RATIONALE

If the language did not require that each EXIT must explicitly specify the label of the compound statement to be terminated (and thus if the EXIT were to terminate the most immediately enclosing compound statement), then a subsequent addition of a new compound statement surrounding the EXIT would change the intended behavior of the program.



NOTES

    An exit statement cannot be used to transfer control out of compound declaration (procedure, function, etc.), since matching identifiers are never inherited by closed scopes.



EXAMPLES

exit examples



6.7  RETURN STATEMENT

return statement diagram
26 - expression

The return statement terminates the invocation of a compound declaration (i.e., procedure, function, task, or capsule).



RULES

Elaboration of the return statement causes elaboration of the smallest enclosing procedure, function, task, or capsule to be terminated.

Expression must be present if the terminated construct is a function, and may not be specified for any other construct. The type of the expression must be the same as that of the function result (see Section 7.2).


RED RATIONALE

The use of a RETURN statement rather than using an EXIT statement was chosen by RED for readability issues, as well as because the result variables of functions can be specified with a type rather than a subtype. This generalization over what was required in Steelman costs very little since it only costs when it is used, and because it allows uniform rules regarding type/subtype specifications on formal parameters and function results.



NOTES

    A return statement is not needed for procedures, tasks, and capsules whose only "return point" is at the end of the body. The elaboration of a function must terminate through a return statement (or by raising an exception).



EXAMPLES

return example



6.8  GOTO STATEMENT

goto statement diagram
C - identifier

The goto statement causes elaboration to continue at a specified statement.



RULES

The identifier must be known as a goto label. Elaboration of the goto statement causes elaboration to continue at the statement labeled by the goto label.



NOTES

    Because the goto label of the target statement must be local, or declared in an enclosing scope, no transfer is allowed into bodies or between alternatives of an if or case statement.

    A goto statement cannot be used to transfer control out of a compound declaration (i.e., procedure, function, etc.), since goto labels are never inherited by closed scopes.



EXAMPLES

goto example






Assignment     Begin     If     Case     Repeat     Exit     Return     Goto

5. Expressions left arrow
x
right arrow 7. Procedures, Functions and Parameters


Overview

Requirements
     Strawman
     Woodenman
     Tinman
     Ironman
     Steelman

RED Reference
RED Rationale

Types in RED
Time/Life Computer Languages
Memories

Site Index

Overview             Reference ToC             Rationale ToC             Site Index



Home   Favorites   Map

IME logo Copyright © 2009, Mary S. Van Deusen