INTRODUCTION TO FORTRAN (FORMULAR TRANSLATION)
Contents
[hide]
Selection[edit]
if[edit]
Fortran has a block-if statement of the form: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 of Fortran'' end if
- Greater than or less than
.GT. .LT. > <
- Greater than or equal to or less than or equal to
.GE. .LE. >= <=
- Equal to and Not equal to
.EQ. .NE. == /=
.AND.
, .OR.
, and .NOT.
:IF ((a .GT. b) .AND.NOT. (a .LT. c)) THEN
program xif implicit none real :: x real, parameter :: x1 = 0.3, x2 = 0.6 call random_seed() call random_number(x) if (x < x1) then print*,x,"<",x1 else if (x < x2) then print*,x,"<",x2 else print*,x,">=",x2 end if end program xif
IF
:IF (''logicalExpression'') GO TO ''lineNumber'' IF (''arithmeticExpression'') ''firstLineNumber'', ''secondLineNumber'', ''thirdLineNumber''
case (switch)[edit]
- select case(...) case (...); ... end select
if (month == "January") then num_days = 31 else if (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
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 default num_days = 30 end select
Loops[edit]
- do i=1,10 ... end do
do i=1,10 print*,i**2 end do
One can exit a loop early using exit, as shown in the code below, which prints the squares of integers until one of the squares exceeds 25.
do i=1,10 isquare = i**2 if (isquare > 25) exit print*,isquare end do
do i=1,10 do ipower=1,3 print*,i,ipower,i**ipower end do end do
DO
, a line number on which the
loop(s) end is used. Here's the same loop, explicitly stating 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
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
do i=start,end,increment ''code goes here'' end do
Simple statements[edit]
GO TO statementNumber
will jump to the specified statement number.STOP conditionCode
will stop with the specified condition 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.
Comments
Post a Comment