You are on page 1of 58

FORTRAN-

FORTRAN stands for Formula


Translation. It was developed by
IBM-Company in 1957 basically
used for scientific problems. The
FORTRAN was known as
FORTRAN II and same was
further modified as FORTRAN –IV.
Further and recent versions are
Fortran-77 and Fortran-90.
Fortran 77, on which we shall hereafter
concentrates, is the most recent
ANSI
(American National Standards Institute)
.The ANSI standard character set for
FORTRAN 77 is given in the
following table:
FORTRAN 77 character set

Character Meaning
0, 1, 2……….,9 Decimal digits
A,B,C……..Z Upper –case
letters
$ Dollar sign
‘ Apostrophe (single quote)
( Left parenthesis
) Right parenthesis
* Asterisk
+ Plus sign
- Minus sign
/ Slash
, Comma
. Period (decimal point)
: Colon
= Equal sign
b Blank or space
Classes of Data

Computer programs,
regardless of language in
which they are written, are
designed manipulate data of
some kind. FORTRAN
provides for two classes of
data- Constants and variables.
Constants

A constant is a quantity whose


value does not change during
program execution.
It may be of numeric,
character, or logical type.
Numeric Type:-
Numeric Type:- Any string
of digits, preceded by a
single algebraic sign, is
called a numeric constant.
General rules for forming
numeric type constants are-

The decimal digits 0,1,2,3,……,9 are


used.
The minus sign must precede a
negative constant; a plus sign is
optional and an unsigned constant
is considered positive.
No commas are to be included in a string of
digits.

Spaces within a constant are allowed, but


their use is discouraged as it is error prone.

The size of a constant is limited by either a


maximum number of digits or a
maximum magnitude.
Numeric Type constants are
further subdivided into integer
and real.
 Integer constant- An integer
constant, also called a fixed point
constant, is a constant which
does not include decimal point.
Thus
26 0 -7 +12345
are valid integer constants.
Real Constant-
A real constant, also called a floating point
constant, is a Constant with a decimal
point and may have a fractional part. The
standard decimal form of real constant
is simply the number written with a
decimal point. Thus
18.3 -123.0 +0.1234
are valid real constants.
Scientific notation
When very small , or very large ,
numbers are involved in hand
computation, it is more convenient
to express them in scientific notation.
Thus
The small number -0.000000076 is
written as -7.6 x 10-8
The large number 20000000000 is also
written as 2 x 1010
Fortran provides an exponential form
for such numbers which is related to
the scientific notation as follows:
( Co-efficient) E (integer)= (co-efficient)
x 10integer
The part appearing before E is called
mantissa and the part following E is
called exponent. The mantissa must
have at least one digit and a decimal
point.
Fortran exponential form

Standard Scientific Fortran


decimal notation exponential
form form

0.0000567 5.67 x10-5 5.67E-5

0.0000000679 6.79 x 10-8 6.79E-8

358900000 3.589 x108 3.589E8


Character Type:-

Any sequence of acceptable characters


in the FORTRAN character set,
enclosed within apostrophes (single
quotes), is called a character constant
or string. The number of characters in a
character constant is the length of the
constant. If an apostrophe is to be one of
the characters of the constants, it must
be entered as a pair of apostrophes.
For example
‘UNIX-PC’ is a character constant of length 7

‘ E. C. I. L.’ is a character constant of length 11


( blanks are characters and are thus included
in character count)

‘DON’’T’ is a character constant of length 5


(apostrophe is a character and is entered as a
pair of apostrophes).
Logical Type:-
In mathematical logic, there are exactly two logical
values, namely,

“True” and “false”.


In Fortran, these are written as

.True. And .False.

.True. Means that the relationship is satisfied and

.False. Means that the relationship is not satisfied.


For example

X.GT.Y is .True. if X is greater than


Y,

And .False. if x is not greater than


Y.
Variables:-

A variable name is the name


used to identify the data
stored in a memory location
whose contents may change
during program execution.
Rules for naming a variable

1. A variable name can contain


letters A to Z and the digits 0 to 9
but no special Characters, such
as +, $,* etc.

2.The first character of a variable


name must be a letter ( A-Z )
3.The total number of
characters in variable name
must not exceed 6.

4.Blank spaces in a variable


name are to be ignored.
Examples

A2X3

ITEM

PAY DAY

are valid variable name.


Reserved words:-
Reserved words, such as READ,
WRITE, PRINT, PAUSE, STOP,
DATA, END, etc. are part of the
FORTRAN language and hence
are not acceptable as variable
name.
Types:-
Variable are of two types:-
Integer
Real

Integer variables:- Integer variables are


represented by variable names starting
with any of the alphabets I,J,K,L,M,N.

For example-
IABC, MASS, MM2
Real variables:-

Real variables are represented by the


variable names starting with any of the
alphabets ( A-H, O-Z).

For example-
AREA, DENSITY, and A2B3
FORTRAN Statements and
Columns
FORTRAN statements are column-
oriented. The line on which a
FORTRAN statement is written
can be considered as being
divided into columns, numbered
from the left, with one character
allowed in each column.
1. Column 1 can contain a C. Whatever
follows after this will be treated as a
comment and will not undergo any
execution at program run time.

2. Columns 1 to 5 can contain a label number.


This provides a convenient way of
referencing this statement from some other
part of the program. The label number does
not have to start in column 1.

3. Column 6 can contain any computer-
acceptable character (except zero or a
blank) to indicate a continuation, i.e. the
previous statement was too long for one
line ( or was cut short for purposes of style)

4. Columns 7 to 72 must contain the


statement proper, but note ( from 3 above)
that continuation lines are allowed. The
statement does not have to start in column
7.
5. Columns 73 onwards are
rarely used. On some
computers they can be used
for comments or line
numbering. Information
beyond column 72 is ignored
at program run time.


Arithmetic Operators

To carry out arithmetic operations a


Fortran program uses the following
symbols:

+ is used for addition

- is used for subtraction


* is used for multiplication
/ is used for division

** is used for exponentiation


( X3.5 is an example of
exponentiation)
Arithmetic Statements

An arithmetic statement consists of


variables and / or array elements and /or
numbers combined together by
arithmetic operators.

In any arithmetic statement there is an


hierarchy of operations:
** are carried out first
* and / are carried out next with
operations proceeding from left to
right
+ and - are carried out next with
operations proceeding from left to
right.
Brackets can be used to override
the normal hierarchy as
operations inside the brackets are
carried out first.
Arithmetic operations must be
separated from one another by
brackets
Thus 7.3** -4.5 would be an error
But 7.3**(-4.5) is acceptable
Note that exponentiation of a negative
number is not allowed, unless the
exponent is an integer.

Thus(-7.3)**3.2 would be an error

But (-7.3)**3 is acceptable.

Arithmetic operations among integers


results in truncation if the result is not
directly an integer, i.e. 10/3 would give 3
as would 11/3.
Library Functions

Many mathematical functions, such


as sine, logarithm etc . , are
contained in the computer library
and can be used as required.

A list of few Fortran library functions


follows-
Example Definition Comment

Exp(x) ex x must be real

Alog(x) ln(x) x must be real


but not zero or

Negative
Alog10(x) log10(x) x must be real
but not zero
Or negative

SQRT (x) Square root of X x must be real


but not negative
ABS(x) Absolute value of x x must be real

Sin(x) Sine(x) X must be


real and in radian

Cos(x) Cosine(x) X must be real


and in radian

ATAN(x) arctangent(x) X must be


real and in radian

TANH(x) hyperbolic tangent(x) X must be


real and in radian
Examples
Arithmetic expression Fortran
expression

SQRT (b**2-4ac) SQRT (B**2-


4*a*c)

cos Ix-y/ x+yI COS(ABS((X-Y)/(X+Y)))

cos(2nπ+x) cos(2*n*3.14+x)
Layout of a FORTRAN Program

A FORTRAN program may consist


of a main segment, or a main
segment together with any of
other segments.
A main segment consists of a
sequence of individual FPRTRAN
statements arranged as indicated
below.
First statement Program name

Initial TYPE statements


Statements COMMON statements
Function statements
OPEN statements
Main group READ Statements
of Statements for inputting
WRITE statements
for outputting
FORMAT
statement information

Arithmetic statements: these carry out


the Calculations
Control statements
GOTO statements

DO statements

IF statements

CALL statements
Progress statements

CONTINUE statements

STOP statements
Last statement End

Comment statements may be


included anywhere in the program.
The Program Statement

The PROGRAM* statement is the first


statement of a FORTRAN program but it
is optional. The PROGRAM statement
gives a name to the main segment.
Example
PROGRAM SPLINE
The name ( SPLINE in the above example
) follows the general rules for names in
FORTRAN programs.
The END statement

This is simply END and must be


the last statement of each
segment.
The STOP Statement

Any number of STOP statements


can be included in a FORTRAN
program. Operation will cease
when any STOP is encountered.
COMMENT Statements

Comments statements have no effect on the


execution of the program but they are an
essential requirement for good
programming style.
A comment statement consists of a C in
column 1 followed by a message. It can
occur anywhere in the program, including
before a PROGRAM statement and
between segments. However there should
be no comment statements after the last
END statement of the whole program.
It is good programming practice to include
extensive comment statements at the start
of each program segment.
Example

C This program adds together two matrices.

C This program is to find out roots


C of quadratic equation.
Type Statements

Type statements are used to declare variables


to be either real or integer.

Examples
REAL Divisor, Max, Theta

INTEGER DEGRC.NUM1, NUM2, TEMP1,


TEMP2
These type statements must be the first of
any initial statements in each program
segment. All variables used in a
segment should be included in type
statements

Note that the word Type does not appear


in a TYPE statement. Type in this
context is a generic name for REAL,
INTEGER and other types that are
available.
When writing a program it is useful
to keep separate lists of real and
integer variables. If variable is
declared in a TYPE statement the
computer will assign a TYPE to it
( names that begin with A to H or
O to Z are typed real, while
names that begin with I to N are
typed INTEGER) but it is not a
good programming.
GOTO Statement:-
This statement performs unconditional
transfer of control, and is used to jump
from one part of the programme to other
part of the programme
Syntax :- GOTO n
where n is the statement label to which the
control is transferred. n can have value
between 1 and 99999.
Input Statement

The READ statement is used to feed values of


the variables.
READ (*,*) variable1, variable2,
e.g.
READ (*,*) A,B,C
The first * in the bracket , by default, reads
data fed from the key board and second *
allows one to feed values of the variables in
the natural format.
Out put Statement

The WRITE statement is used to display


values of the variables calculated with the
computer.
WRITE (*,*) variable1, variable2,
e.g.
WRITE (*,*) X, Y, Z
The first * in the bracket, by default, writes
values of the variables, X, Y, Z on the
computer, and second * prints them in the
natural format.
IF Statement:-
Several types of IF statements are used in
FORTRAN programming. These are
necessitated by the constraints of the
problem. The IF statements are:
1. Arithmetic IF
2. Logical IF
3. Block IF
4. Nested Block IF
5. Multi alternative IF
Arithmetic IF:-
Syntax of this statement is:
IF ( Arithmetic expression)n1,n2,n3
If the numeric value of Arithmetic expression
is –ve , computer transfer the control to
statement label n1
If the numeric value of Arithmetic expression
is Zero , computer transfer the control to
statement label n2
If the numeric value of Arithmetic expression
is +ve , computer transfer the control to
statement label n3

You might also like