== 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: . ''label ...... ''or<
> ''label ''CONTINUE<
> ''label'' -> Number consisting of 1 up to 5 digits. At least 1 digit must be different from '0' 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 1. 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: . GOTO (label1 [,label2] ...[labeln]) expr<
> ''label'',,''1..n'',, It is jumped to the address specified by labelN, if expr has value N<
> ''expr'' arithmetic. expression of type INTEGER '''Please see Example 2''' '''Please do Task 6.1''' . GOTO <''label''><
> STOP n<
> ''label'' instruction number (1-5 digits) 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. . GOTO (label1 [,label2] ...[labeln]) expr<
> ''label'',,''1..n'',, It is jumped to the address specified by labelN, if expr has value N<
> ''expr'' arithmetic. expression of type INTEGER '''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. . IF ''( expr ) label1,label2,label3''<
> ''expr'' numerical expression or variable (not COMPLEX!) <
> ''label'',,''1,2,3'',, label to be jumped to if expr is smaller than 0 (label1), equal 0 (label2) or great equal 0 <
> (label3). '''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. . IF (expr) THEN <
> >> FORTRAN - instructions (for expr = = .true.) <<<
> ELSE <
> >> FORTRAN - instructions (for expr = = .false.) <<<
> ENDIF<
> expr expression or variable of type LOGICAL 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''' . IF (expr) THEN <
> >> FORTRAN - statement <<<
> ENDIF <
> expr expression or variable of type LOGICAL '''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. . IF (expr,,1,,) THEN <
> >> FORTRAN - instructions (for expr,,1,, = = .true.) <<<
> ELSEIF (expr,,2,,) THEN <
> >> FORTRAN - instructions (for expr,,2,, = = .true.) <<<
> ELSEIF (expr,,3,,) THEN <
> >> FORTRAN - instructions (for expr,,3,, = = .true.) <<<
> ELSE <
> >> FORTRAN - instructions (if no other condition is true) <<<
> ENDIF<
> expr expression or variable of type LOGICAL '''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. . [''name:''] DO [''label''] or<
> [''name:''] DO [''label''] [,] WHILE (expr) or <
> [''name:''] DO [''label''] [,] ''index'' = I1,I2,[,I3] <
> instructions<
> ''label'' CONTINUE or <
> END DO [''name'']<
> ''label'' instruction number of an executable statement (e.g. CONTINUE) representing the last<
> instruction within the loop.<
> ''index'' loop control variable<
> I1 initial value of the control variable index<
> I2 final value of the control variable index <
> I3 increment, default=1<
> ''name'' name of a DO-loop, which is terminated by an END DO statement (optional)<
> ''expr'' expression or variable of type LOGICAL. The loop is carried out as long as expr has <
> value .TRUE. 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). * At some compilers a loop is executed at least one time even if I2 < I1. * Loops can be nested, but they must be completely located inside the (outer) loop. * Several nested loops may end at the same terminating instruction ('label CONTINUE') * Jumps aren't permitted into loops. 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: . Variable, index = i,,1,,,i,,2,,,[i,,3,,])<
> Variable list of variables, (in most cases an array) <
> index control variable <
> i,,1,, initial value of the control variable index<
> i,,2,, final value of the control variable index <
> i,,3,, increment, default=1 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: . Variable, index = i,,1,,,i,,2,,,[i,,3,,])<
> Variable list of variables, (in most cases an array) <
> index control variable <
> i,,1,, initial value of the control variable index<
> i,,2,, final value of the control variable index <
> i,,3,, increment, default=1 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. . [<''name''> :] SELECT CASE''(expr)'' <
> CASE (selection,,1,,) [] <
> ...<
> CASE (selection,,2,,) [] <
> ...<
> [CASE DEFAULT]<
> END SELECT [] <
> ''expr'' control-expression of type INTEGER, Logical or CHARACTER<
> ''selection'',,''i'',, list of values (of type of ''expr'')<
> ''name'' name of the CASE-statement 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. . WHERE ''(mask-expression) assignment'' <
> WHERE ''(mask-expression)'' <
> ''assignment'',,''1'',, <
> [ELSEWHERE<
> ''assignment'',,''2'',,]<
> END WHERE<
> ''mask-expression'' expression or array of type LOGICAL which has the same shape as the <
> operands of the assignment operation<
> ''assignment'',,''i'',, assignment operation for elements which are associated with a .TRUE.<
> element of the mask-expression '''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. . [name:] FORALL I=I,,1,,:I,,n,,:INC,... [,log. cond.])<
> FORALL-statement or <
> WHERE-statement or -instruction-group or<
> FOREALL-statement or instruction-group or<
> Anweisungen<
> END FORALL [name]<
> <
> INC Increment for index<
> log. cond. logical condition for executing the instruction '''Please see Example 15'''