Fortran notes-1

 

CONSTANTS AND VARIABLES

The variable names that begin with the letters I,J,K,L,M,N are considered integers unless they are defined otherwise.

integer a

real b

double precision dtemp(50)

complex cx(100)

character*4 code, name

character*(*) name(n)

parameter (pi=3.141593)

 

Arithmetic operations

 

Pi=3.141593

Addition

A+B

A+B

Subtraction

A-B

A-B

Multiplication

A x B

A*B

Division

A : B

A/B

Exponentation

A

A**3

 

Intrinsic functions

SQRT(X)

Square root of x

LOG(X)

Natural log of x

ABS(X)

Absolute value of x

LOG10(X)

Common log of x

SIN(X)

Sine of x (in radians)

INT(X)

Integer part of x-converts a real value to an integer value

COS(X)

Cosine of x (in radians)

REAL(I)

Real value of I-converts an integer value to a real value

TAN(X)

Tangent of x(in radians)

MOD(I,J)

Integer remainder of I/J-Remainder or modulo function

EXP(X)

e raised to the x power

 

 

 

Integer sum, n

Real averg

Averg= real(sum)/real(n)

 

Simple input and output

 

print*, weight, vol

read*, begin, ending

 

Formatted output

Print 5, time, distinc

5 format (1X,F5.1,2X,F7.2)

 

For integer values we use : I3 meaning 3 characters

F-specification: Is used to print real numbers in a decimal form.

Fw.d

XX.XXX total width=w=6

Decimal portion=d=3

E-specification:Real numbers may be printed in an exponential form

Ew.d

S0.XXXESXX

 

The symbol S indicates that positions must be reserved for both the sign of the value and the sign of the exponent in case they are negative.

Total width=w=10

Decimal portion=XXX=3

 

Complete programs

PROGRAM TEST

.....

STOP

END

 

 Logical expressions

.EQ.

Equal to

.LE.

Less than or equal to

.NE.

Not equal to

.GT.

Greater than

.LT.

Less then

.GE.

Greater than or equal to

 

 

.AND.

.OR.

.NOT.

 

Logical if statement

 If (a.lt.b) sum=sum+a


If (a.eq.b) then

Print*, "hello"

End if


If (logical expression 1) then

Statement 1

...

statement n

if (logical expression 2) then

statement n+1

...

statement m

end if

statement m+1

...

statement p

end if


 

 If (logical expression 1) then

Statement 1

...

statement n

else

Statement n+1

...

statement m

end if


If (logical expression 1) then

Statement 1

...

statement m

else if (logical expression 2) then

Statement m+1

...

statement n

else if (logical expression 3) then

Statement n+1

...

statement p

else

Statement p+1

...

statement q

 

end if


WHILE LOOP STRUCTURE

n IF (logical expression) THEN

Statement 1

...

statement m

GO TO n

END IF

 

DO LOOP

Do k index=initial,limit, increment

....

k continue

 

Example:

The sum of 1+2+3+...+50

Sum=0

Do 10 number=1,50

Sum=sum+number

10 continue

 

 

Back to the physics homepage

Back to my homepage