Previous | Table of Contents | Next |
Almost any useful program has the properties that some collections of statements are executed many times, and different sequences of statements are executed depending on the values of the input data.
The Fortran statements that control which statements are executed, together with the statements executed, are called control constructs.
There are three kinds of control constructsthe if construct, the case construct, and the do construct.
1.2.13.1. Statement Blocks
A collection of statements whose execution is controlled by one of the control constructs is called a block. For example, the statements between an if statement and the next matching else if statement form a block. Transferring control into a block from outside is not allowed, but it is possible to leave a do construct with a transfer of control, such as an exit or cycle statement. Any block may contain a complete if, case, or do construct so that these constructs can be nested to any level.
1.2.13.2. The if Construct
The if construct is a simple and elegant decision construct that permits the selection of one of a number of blocks during execution of a program. Some simple examples follow:
if (dice <= 3 .or. dice == 12) then print *, You lose! else if (dice == 7 .or. dice == 11) then print *, You win! else print *, You have to keep rolling until you get print *, either a 7 or a, dice end if ! 30 days has September, April, June, and November if (month == 9 .or. month == 4 .or. & month == 6 .or. month == 11) then number_of_days = 30 ! All the rest have 31, except February else if (month == 1 .or. month == 3 .or. & month == 5 .or. month == 7 .or. & month == 8 .or. month == 10 .or. & month == 12) then number_of_days = 31 else if (month == 2) then if (leap_year) then number_of_days = 29 else number_of_days = 28 end if else print *, month, is not the number of a month. end if
The logical expressions in the if statement and the else if statements are tested until one is found to be true. Then the block following the statement containing that test is executed, which completes execution of the if construct. If all logical conditions are false, the block following the else statement is executed (if there is one).
1.2.13.3. The case Construct
The case construct is somewhat similar to the if construct in that it permits selection of one of a number of different alternative blocks of instructions, providing a streamlined syntax for an important special case of a multiway selection.
The case construct is executed by evaluating the expression in the select case statement. Then the expressions in the case statements are examined until one is found with a value or range that includes the value of the expression. The block of statements following this case statement is executed, completing execution of the entire case construct. Unlike if constructs, no more than one case statement can match the value of the expression. If no case statement matches the value of the expression and there is a case default statement, the block following the case default statement is executed.
Any of the items in the list of values in the case statement can be a range of values, indicated by the lower bound and upper bound separated by a colon (:). The case expression matches this item if the value of the expression is greater than or equal to the lower bound and less than or equal to the upper bound.
Some simple examples follow:
select case (dice) case (2:3, 12) print *, You lose! case (7, 11) print *, You win! case default print *, You have to keep rolling until you get print *, either a 7 or a , dice end select select case (traffic_light) case (red) print *, Stop case (yellow) print *, Caution case (green) print *, Go case default print *, Illegal value:, traffic_light end select select case (month) ! 30 days has September, April, June, and November case (9, 4, 6, 11) number_of_days = 30 ! All the rest have 31, except February case (1, 3, 5, 7, 8, 10, 12) number_of_days = 31 case (2) if (leap_year) then number_of_days = 29 else number_of_days = 28 end if case default print *, month, is not the number of a month. end select select case (symbol) case (a:z) category = lowercase letter case (A:Z) category = uppercase letter case (0:9) category = digit case default category = other end select
Previous | Table of Contents | Next |