0% found this document useful (0 votes)
66 views3 pages

Fortran/Fortran Control: 1 Selection

Fortran uses IF statements and logical expressions to control program flow. It has block IF statements that can test multiple logical expressions. It also uses SELECT CASE statements as an alternative to chained IF-ELSEIF blocks when testing a single variable. Fortran uses DO loops to iterate. DO loops can be nested and use start, end, increment arguments that can be variables. Simple statements like GO TO, STOP, EXIT, CONTINUE, and CYCLE also control flow.

Uploaded by

Jôssŷ Fkr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views3 pages

Fortran/Fortran Control: 1 Selection

Fortran uses IF statements and logical expressions to control program flow. It has block IF statements that can test multiple logical expressions. It also uses SELECT CASE statements as an alternative to chained IF-ELSEIF blocks when testing a single variable. Fortran uses DO loops to iterate. DO loops can be nested and use start, end, increment arguments that can be variables. Simple statements like GO TO, STOP, EXIT, CONTINUE, and CYCLE also control flow.

Uploaded by

Jôssŷ Fkr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Fortran/Fortran control

Part of the Fortran WikiBook IF (''logicalExpression'') GO TO ''lineNumber'' IF


(''arithmeticExpression'') ''firstLineNumber'', ''second-
LineNumber'', ''thirdLineNumber''
1 Selection
In the first form, things are pretty straightforward. In the
1.1 if second form, the arithmetic expression is evaluated. If
the expression evaluates to a negative number, then exe-
if (...) then ... else ... end if cution continues at the first line number. If the expression
evaluates to zero, then execution continues at the second
Fortran has a block-if statement of the form: line number. Otherwise, execution continues at the third
line number.
if (logical expression1) then ''Lines of Fortran'' else
if (logical expression2) then ''Lines of Fortran'' else if
(logical expression3) then ''Lines of Fortran'' else ''Lines 1.2 case (switch)
of Fortran'' end if
select case(...) case (...); ... end select
The following operators can be used when making the
logical expression: If an if block consists of repeated tests on a single vari-
able, it may be possible to replace it with a select case
• Greater than or less than construct. For example, the code
if (month == “January”) then num_days = 31 else if
.GT. .LT. > < (month == “February”) then num_days = 28 print *,"You
can put more stuff here.” else if (month == “March”)
then num_days = 31 else num_days = 30 end if
• Greater than or equal to or less than or equal to
can be replaced by
.GE. .LE. >= <=
select case (month) case (“January”) num_days = 31
case (“February”) num_days = 28 print *,"You can put
more stuff here.” case (“March”) num_days = 31 case
• Equal to and Not equal to
default num_days = 30 end select
.EQ. .NE. == /=
Fortran does not need a break statement.
To check more than one statement, use .AND., .OR., and
.NOT.:
2 Loops
IF ((a .GT. b) .AND.NOT. (a .LT. c)) THEN
do i=1,10 ... end do
The following program generates a random number be-
tween 0 and 1 and tests if it is between 0 and 0.3, 0.3 and
0.6, or between 0.6 and 1.0. To iterate, Fortran has a do loop. The following loop
prints the squares of the integers from 1 to 10:
program xif implicit none real :: x real, parameter :: x1 =
0.3, x2 = 0.6 call random_seed() call random_number(x) do i=1,10 print*,i**2 end do
if (x < x1) then print*,x,"<",x1 else if (x < x2) then
print*,x,"<",x2 else print*,x,">=",x2 end if end program One can exit a loop early using exit, as shown in the code
xif below, which prints the squares of integers until one of
the squares exceeds 25.
There are two interesting archaic forms of IF: do i=1,10 isquare = i**2 if (isquare > 25) exit

1
2 3 SIMPLE STATEMENTS

print*,isquare end do

Loops can be nested. The following code prints powers 2


through 4 of the integers from 1 to 10
do i=1,10 do ipower=1,3 print*,i,ipower,i**ipower end
do end do

In an archaic form of DO, a line number on which the


loop(s) end is used. Here’s the same loop, explicitly stat-
ing that line 1 is the last line of each loop:
DO 1 i=1,10 DO 1 ipower=1,3 1 PRINT
*,i,ipower,i**ipower

If using the archaic form, the loop must not end on an


IF or GO TO statement. You may use a CONTINUE
statement instead in these cases.
There is also an optional increment argument when
declaring a do loop. The following will count up by two’s.
2, 4, 6, ...
do i=2,10,2 write (*,*) i end do

Arguments to the do loop don't have to be numbers, they


can be any integer that is defined elsewhere in the pro-
gram. start, end, and increment can be any variable name.
do i=start,end,increment ''code goes here'' end do

3 Simple statements
GO TO statementNumber will jump to the specified state-
ment number.
STOP conditionCode will stop with the specified condi-
tion code or exit code. STOP may be coded without an
argument. Note that on many systems, STOP 0 is still a
failure.
EXIT will leave a loop.
CONTINUE can be used to end an archaic DO loop when
it would otherwise end on an IF.
CYCLE will transfer the control of the program to the
next END DO statement.
RETURN leaves a subroutine or function.
3

4 Text and image sources, contributors, and licenses


4.1 Text
• Fortran/Fortran control Source: https://en.wikibooks.org/wiki/Fortran/Fortran_control?oldid=2464983 Contributors: Beli-
avsky~enwikibooks, Kernigh, Dingar, Kaos, H2g2bob, Jguk, Arthurv, AOL IP address 152.146.34.112, Slinker, Dpryan, Kghongaku and
Anonymous: 8

4.2 Images

4.3 Content license


• Creative Commons Attribution-Share Alike 3.0

You might also like