Department numbers: Difference between revisions
→{{header|Fortran}}
Line 515:
This uses the ability standardised in F90 of labelling a DO-loop so that its start and end are linked by usage of the same name, with this checked by the compiler. Further, in avoiding the use of the dreaded GO TO statement, the CYCLE statement can be employed instead with the same effect, and it too can bear the same name so that it is clear which loop is involved. These names prefix the DO-loop, and so, force some additional indentation. They are not statement labels and must be unique themselves. Notably, they cannot be the same text as the name of the index variable for their DO-loop, unlike the lead given by BASIC with its <code>FOR I ... NEXT I</code> arrangement.
The method is just to generate all the possibilities, discarding those that fail the specified tests.
Since the modernisers of Fortran made a point of specifying that it does not specify the manner of evaluation of compound boolean expressions, specifically, that there is to be no reliance on [[Short-circuit_evaluation]], both parts of the compound expression of the line labelled 5 "may" be evaluated even though the first may have determined the result. Prior to the introduction of LOGICAL variables with F66, one employed integer arithmetic as is demonstrated in the arithmetic-IF test of the line labelled 6. On the B6700, this usage ran faster than the corresponding boolean expression - possibly because there was no test for short-circuiting the expression when the first part of a multiply was zero...
SS:DO S = 1,7 !The sanitation department accepts any value.▼
Note that the syntax enables ''two'' classes of labels: the old-style numerical label in columns one to five, and the special label-like prefix of a DO-loop that is not in columns one to five. And yes, a line can have both. <lang Fortran> INTEGER P,S,F !Department codes for Police, Sanitation, and Fire. Values 1 to 7 only.
1 PP:DO P = 2,7,2 !The police demand an even
4 F = 12
5 IF
6
7 WRITE (6,"(3I2)") P,S,F !If we get here, we have a possible set.
END DO PP !Next P.▼
8 END DO SS !Next S
END !Well, that was straightforward.</lang>
Output:
|