Department numbers: Difference between revisions

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. <langHowever, Fortran>the requirement that the codes add INTEGERup P,S,F !Departmentto codestwelve formeans Police,that Sanitationafter the first two are chosen the third is determined, and Fire.blandly Valueslooping 1through toall the possibilities is too much brute force and ignorance, though other collections of rules could make 7that onlybearable.
 
PP:DO P = 2,7,2 !The police demand an even number. They're special and use violence.
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.
 
IF (P.EQ.S) CYCLE SS !But it must differ from the others.
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.
FF:DO F = 1,7 !The fire department accepts any number.
1 PP:DO P = 2,7,2 !The police demand an even IF (Fnumber.EQ.S) CYCLEThey're FF !Butspecial itand mustuse differviolence.
2 SS:DO S = 1,7 !The sanitation department accepts any value.
IF (F.EQ.P) CYCLE FF !And not be the police's number also.
3 IF (P + S + F .NEEQ. 12S) CYCLE FFSS !And, ...,But theyit must adddiffer upfrom tothe twelveothers.
4 F = 12 WRITE- (6,"(3I2)")P + P,S,F) !IfThe wefire getdepartment hereaccepts any number, webut the havesum amust possiblebe settwelve.
5 IF END(F.LE.0 DO.OR. FF F.GT.7) CYCLE SS !NextEnsure Fthat the only option is within range.
6 END DOIF SS((F - S)*(F - P)) 7,8,7 !NextAnd F it must differ from S and from P
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
9 END DO PP !Next P.
END !Well, that was straightforward.</lang>
Output:
1,220

edits