LehreWiki

6 Control constructs

Control structures are those FORTRAN statements which control the program flow, e.g. control branching, jumps and cycle structures. Labels play an important role at this.

6.1 Label

A label (or instruction number) represents an address, which is used as the target of a jump, an indicator of an FORMAT statement or even the end of a DO loop. A label is used in the form:

A label consists of one up to five digits while at least one digit must be non-zero. A specific instruction number must appear in a program unit only once (it must be unique within a program unit).

6.2 CONTINUE statement

The CONTINUE statement is an 'empty' instruction which doesn't cause any specific action. It is meaningful only as an address for a GOTO-jump or keeping an 'end of a loop' label.

  1. STOP statement
  2. STOP [message] or
    STOP n
    message text string (or a sequence with up to 5 digits) that is displayed (on standard output)
    when the STOP statement is executed

The stop STOP statement causes the termination of the program. It may keep a message that is displayed at standard output when the program terminates. A program unit can contain multiple STOP statements.

6.4 Jump statements

Jumps statements are used to force the program to continue execution at a defined position and not to proceed sequentially (i.e. follow the order of the instruction in the program source code).

6.4.1 Unconditional jump instruction

An unconditional jump instruction causes a jump to an address which is specified by an instruction number (label), and has the form:

Please see Example 2

Please do Task 6.1

The statement causes the program to proceed execution at the instruction line with the label specified by the GOTO statement.

Please see Example 1

6.4.2 Computed jump instruction

At a computed jump instruction various jump destinations can be specified. The (list position or the) jump label is depending on the value of an arithmetical expression.

Please see Example 2

Please do Task 6.1

6.5 Branching

Branching permits to permits to branch to different statements or statement blocks depending on the value of a (logical) variable or the (logical) result of an expression. This is made by use of the IF statement.

6.5.1 Arithmetic IF-statement

The arithmetical IF statement permits to select a branching dependent if the value of an expression or variable is greater than, equal or less than 0., i.e. one of the three labels specified is used in any case.

Please see Example 4

6.5.2 Logical IF-statement

A logical IF instruction permits the execution of any statement depending on the result of a logical expression. Such an instruction can be the GOTO statement.

IF ( expr ) statement
expr expression or variable of type LOGICAL
statement FORTRAN statement

Please see Example 5

6.5.3 Block IF-construct

By use of a block IF construct not only a single instruction can be carried out (as for a logical IF) but a block of instructions can be executed depending of the value of a logical expression. All instructions which appear between the THEN and the ENDIF keyword will be executed, if the logical expression has value .TRUE.

By using the ELSE statement it is possible to define an alternative block of statements which will be executed if the expression for the IF control has the value .FALSE.

In an IF-THEN-ELSE construct, either the THEN-ELSE or the ELSE-IF block is executed. An ELSE instruction can appear only once in an IF-ENDIF structure.

Please see Example 7

Please see Example 6

6.5.4 ELSE-statement

6.5.5 ELSEIF-statement

The ELSEIF statement permits further case distinctions within an IF construct. Only the first instruction block for which the logical (control-) expression is .TRUE. is executed - otherwise the ELSE block is executed (if specified). Then execution is continued after the ENDIF statement. ELSEIF blocks may appear repeatedly.

Please see Example 8

6.6 Loops

Loops are used for executing a block of instructions repeatedly. Loops are declared with the FORTRAN statements DO - END DO.

The control variable (index) keeps the value which it has in the loop during the last cycle. At normal run this is (in general) the final value of the control variable (index) increased by the increment (i.e. I2+I3).

6.6.1 Nested Loops

Loops can be nested into each other. A loop must always be completely included into an 'outer' loop while nested loops may have the same terminating statement (when using a label as a loop delimiter).

Please see Example 9

6.6.2 CYCLE and EXIT-statment in loops

The statement CYCLE causes the end of the current loop execution. The current cycle is terminated and a jump to the end of the loop (e.g. END DO) is carried out. Loop execution is continued with the next loop index value.

The EXIT statement causes a complete termination of the loop such as the processing of the loop.

When making use of loop the naming possibility of the CYCLE and EXIT statement may also be applied to outer loops (by specifying their name).

Please see Example 10

6.6.3 Implicit Loops in I/O-statements

In I/O-lists of READ, WRITE, PRINT statements, loop constructs may be used too. These (implicit loops) have the form:

Implicit loops may also be applied in DATA-statements or in array constructors.
Implicit loops can be nested too (see below).

Please see Example 11

6.6.4 Implicit loops in DATA-statements

Implicit loops also can be used in order to initialise arrays or sub-arrays by means of a DATA statements. The syntax is identical to that for implicit loops used in I/O statements.

Please see Example 12

Please do Tasks 6.2, 6.3

6.6.3 Implicit Loops in I/O-statements

In I/O-lists of READ, WRITE, PRINT statements, loop constructs may be used too. These (implicit loops) have the form:

Implicit loops may also be applied in DATA-statements or in array constructors.
Implicit loops can be nested too (see below).

Please see Example 11

6.6.4 Implicit loops in DATA-statements

Implicit loops also can be used in order to initialise arrays or sub-arrays by means of a DATA statements. The syntax is identical to that for implicit loops used in I/O statements.

Please see Example 12

Please do Tasks 6.2, 6.3

6.7 CASE-statement

The CASE statement permits a branching of the program due to case distinctions which may also be executed by means of IF instructions in a much more circumstantial way. If the (control-) expression specified in the SELECT CASE statement is identical with a given value of (selection''i'')-list for a specific CASE block, this CASE block will be executed. If no valid selection value is found, the CASE DEFAULT block (if available) will be executed. Only one (the first valid) CASE block found is executed.

A list of values may contain ranges of values (REAL, CHARACTER) that are specified by a first and/or a last value of the range.

strat_value : end_value

All values between start_val and end_val

-9 : 12

start_value

All values greater equal start_val

'm': All character with ASCII code <= ASCII code of 'm'

: end_value

All values smaller equal end_val

:'z' All character with ASCII code >= ASCII code of 'z'

In case of CHARACTER ranges, the ASCII code number is used for ordering the characters.

Please see Example 13

Please do Tasks 6.4, 6.5, 6.6

6.8 Masked array assignments (WHESE)

Masked array assignments are carried out with the WHESE statement. It is used for array operations mostly. It can execute assignments dependent on whether a definite condition applies for an element, an array or a sub-array. This condition also can be a mask which represents an array of logical elements. The WHESE statement does not represent a real control structure but it can replace multiple loops for a multi- dimensional array.

Please see Example 14

6.9 FORALL - Statement

In FORTRAN 95 the FORALL-statement is introduced. It permits an explicit control of loops. The FORALL statement is an 'universal' extension of the WHERE statement and allows to assign values to groups of array elements.

Please see Example 15

LehreWiki: Chapter_06 (last edited 2010-03-31 08:15:09 by HansLuthardt)