FORTRAN
FORTRAN stands for FORmula TRANslation. It is one of the oldest computer programming languages, developed in
1954, by John Backus for IBM. It is most widely used high level programming language and is being used mainly
for mathematical or numerical or scientific applications. The history of FORTRAN’s versions can be classified
as follow.
- FORTRAN 1957
- FORTRAN II
- FORTRAN IV
- FORTRAN 66
- FORTRAN 77
- FORTRAN 90
- FORTRAN 95
And the most common and popular version of FORTRAN today is still Fortran 77. [A Brief History of
FORTRAN]
ForTran is included in the curriculum of TU, IOE, BE (all programs) as topic of syllabus of COMPUTER
PROGRAMMIG. The weight of this topic is 12 out of 80 marks and the allocated time duration is 8 hours.
According to the syllabus, we should study about C programming language before studying ForTran. Concept of
writing algorithm, drawing flowchart, flowcharting symbols and fundamental of programming is independent of
any languages. It means, we are already familiar to those common topics. Therefore, in this text we mainly
focus on the syntactic structure of ForTran programming language. Let us compare a basic example of a
ForTran program to C program. We can write ForTran syntax either in all upper case or all in lower case or
in both.
FORTRAN Program Structure
- Documentation section
- Program name
- Declaration of variables
- Variable initialization
- Program statement
- End
Documentation section:
The documentation section tells the user what the program does, who wrote it,
dates written and revision history. For documentation, we can use either c,* or !. To use C or * for
commenting it must be placed at the first column of the ForTran coding sheet but symbol ! can be placed
anywhere except inside single quotes.
Program name:
The first line of the programs generally gives the program’s name. For example, program
sum indicates that the following program is about addition. Here program is a ForTran verb and sum is
identifier. We can write whatever we like. In fact, this part of a ForTran program is optional. But it is good
practice to name every program.
Declaration of variables:
In this we must declare all the variables, which set up space in computer
memory for the data types that will be used in the program.
Variable Initialization:
After the declaration of variables, our computer knows how much space is to
be allocated for each variable, but the actual value of this variable is not known. Although some ForTran77
compilers implicitly assign each declared variable a zero value or an empty string (for character types), it
is strongly recommended to initialize variables if any variable needs to set some initial value.
Program statement:
Program statements are the actual steps that solve the original problem. Comment
lines can also be included in order to describe what the line of code actually does.
End:
End is the last statement of the ForTran program. It indicates the end of the program. Every
ForTran program must be terminated by the end statement.
ForTran Coding Sheet
It is a sheet of paper used to write ForTran program.
While writing FORTRAN statements, it requires text and statements to be placed in specific area of each line.
Why?
- It has 80 columns.
- Each line is divided into five distinct zones as described below.
- Column 1: Used for writing comment. Character c or * indicate the line as comment line.
- column 1 – 5: Used for giving line numbers for other lines of program. The line number is a positive
integer with at most five digits. Every statement need not be given the line number.
- column 6: Used for continuation i.e. if a statement does not fit in columns 7 to 72, the remaining part
must be written in the next line. We can use + sign as a continuation mark in position 6 which means both
lines represent single statement, not two separate statements. Any character can be used instead of the
plus sign as a continuation character. It is considered good programming style to use the plus sign or an
ampersand or digits (using 2 for the second line, 3 for third, and so on).
- column 7 – 72 : For writing ForTran statements.
- column 73 – 80 : Used for comment for the programmer if needed. The compiler does not read columns 73 to
80.
Character Set
The character set includes letters from A to Z or a to z. Similarly, digits from 0 to 9 and symbols
+-/*.,’_$().
FORTRAN Verbs / Keywords
What is the role of keywords in C? There are some words in ForTran having the similar role are called
ForTran verbs. For example, program, character, integer, real, double precision, if, then, else, while, end
etc.
Data Types
ForTran program supports following standard data types.
Constants
Constant has a value which is fixed when the program is written. It is called constant as its value doesn’t
change during program execution.
Integer Constant
Integer numbers written without decimal point are called fixed-point constants or integer constants. Integers
are formed by digits 0,1,2,3,4,5,6,7,8,9 and + or – symbol. The sign symbol occurs at the leftmost position of
the number. If the number is positive the plus sign is optional.
For example: +1234, 420, -10
Real Constant
If a numbers has a fractional part is called real or floating point constant. Such numbers are written with
one decimal point.
For example: 3.1415, 2.5, 1.123E13
Character Constant
These constants are similar to the string constants in C. But in case of ForTran these constants are enclosed
by single quotation marks.
For example: ‘Patan’, ‘Pulchowk campus’
Logical Constant
There are only two possible logical constants, and they are expressed as: .TRUE. and .FALSE. The dots at each
end are needed to distinguish these special forms from the words TRUE and FALSE.
Complex Constant
A complex constant has real and imaginary component separated by a comma and enclosed in a pair of
parentheses.
For example: (2.14, -3.67) (2,4)
Variables
The concept of variable is already studied in C. Actually variable are the name of memory location where the
data are stored for computing. The first character of any variable name must be an alphabet. ForTran verbs
cannot be used for variable. Similarly, any special characters are not allowed in variable names. For example,
7sum, goto, r-e are some invalid variable names. It is better to give meaningful variable name.
Example:
integer i,j
real area
character name*20
Defining data types for a variable
i. Explicit definition
We can define variable explicitly by using the data type like:
integer i,j,k
real x,y
ii. Implicit definition
We can define a variable without explicit data type in variable declaration statement. Older versions of
ForTran allowed a feature called implicit definition, i.e. you do not have to declare the variables before
use. If a variable is not declared, then the first letter of its name will determine its type.
Variable names starting with
i,j,k,l,m,n are considered to be for integer variable and others are
real variables. However, you must declare all the variables as it is good programming practice. For that you
start your program with the statement – implicit none. This statement turns off implicit definition.
Q. WAP showing
example of implicit definition?
Operators and Expression
Operators are used in programs to perfrom certain mathematical or logical manipulations. For example, in a
simple expression 8+9, the symbol + is called an operator which operates on two data items 8 and 9. And the
data items that operators act upon are called operands. So here, 8 and 9 are operands.
An expression is a combination of variables, constants and operators written according to syntax of the
language. For example, the following are examples of expressions:
9 + 8, a+b*c, a>b, a*b/3
Types of Operators
1. Arithmetic Operators
The arithmetic operator perform arithmetic operation.
Arithmetic Operators |
Purpose |
+ |
Addition (a+b) |
- |
Subtraction (a-b) |
* |
Multiplication (a*b) |
/ |
Division (a/b) |
** |
Exponentiation (n**2) |
2. Relational Operator
The relational operators are the symbols which is used to compared two or more subject matter.
Relational operator |
FORTRAN operator |
Meaning |
<< /td>
| .LT. |
(smaller than) a .lt. 5 |
> |
.GT. |
(greater than) a .gt. 5 |
<=< /td>
| .LE. |
(smaller than equal to) a .le. 5 |
>= |
.GE. |
(greater than equal to) a .ge. 5 |
== |
.EQ. |
(equal to) marks .eq. 32 |
!= |
.NE. |
(not equal to) mark .ne. 32 |
3. Logical Operator
The logical operator are used to combine two or more than two expression of relational operator.
Logical operators |
Purpose |
.AND. |
if(a .GT. b .AND. a .GT. c) |
.OR. |
if(marks .LT. 32 .OR. attendance .LT. 80) |
.NOT. |
.NOT. .TRUE. |
4. Assignment Operator
Assignment operator are used to assigne a value of right hand variable to left hand variable.
Example:
A = l x b
sum = a + b
5. String Concatenation Operator
Two Character strings can be joined together into a single string using concatenation operator.
The concatenation operator is represented by a double forward slash //.
It is also known as character operator.
Control Statement in FORTRAN
The statement which alter the flow of execution of the program are known as control statements. The
control statement may be either decision making or loop / repetition.
Decision Making Statements
The decision making statement decides to execute certain statements or not. The decision making types of
control statements are classified as following:
- Logical if (if else, if else ladder, nested if else)
- Arithmetic if
- Unconditional goto statement
- Computed goto statement
i. Logical if
The concept of logical if is similar to if (along with if else, if else ladder and nested if else statement)
statement in C programming. The general form of this statement in FORTRAN is:
if (test condition) then
statement 1
… …
else
statement 2
… …
endif
If condition produces true value, the statement 1 is executed, otherwise statement2 is executed.
ii. Arithmetic if
The arithmetic if statement is used to transfer the program control to a particular statement in the program
depending upon the value of an expression whether it is negative, zero or positive. The general form of
Arithmetic if is:
if (expression) st_no1, st_no2, st_no3
where expression is any valid FORTRAN arithmetic expression and stn1, stn2, and stn3 are the statement
numbers.
If the value of the expression is negative, control goes to the statement number st_no1,
If the value of the expression is zero, control goes to the statement number st_no2
If the value of the expression is positive, control goes to the statement number st_no3
iii. Unconditional goto statement
This statement is used to transfer the program control to any other statement unconditionally. Its general
form is:
goto st_no
where, st_no is the statement number where the program control is transferred to any number of statement
without checking any condition. The value of st_no must not be floating point number, zero or negative. It can
be five digits at most. There must be one statement number for each statement.
iv. Computed goto statement
The computed goto statement transfers program control to one of several statements specified, depending on the
value of an integer. The general form of this type of goto statement is:
goto (st_no1, st_no2, st_no3,…st_n), i
where, st_no1, st_no2, st_no3 … n are statement numbers. These must be enclosed within parenthesis. The
variable i is the single integer variable. There must be a comma between closing parenthesis and the integer
variable like i.
if i = 1, control goes to statement number st_no1
if i = 2, control goes to statement number st_no2
if i = 3, control goes to statement number st_no3
… …
if i = n, control goes to statement number st_n
It is important to note that if i < 1 or i>n, the goto statement is ignored and control transfers to next
statement line.
Looping / Repetition in FORTRAN
Loop may be defined as block of statements which are repeatedly executed for a certain number of times or
until a particular condition is satisfied. The FORTRAN 77 has only one loop: do loop, supported by ANSII
standard. However most of compilers supports following types of loops.
- i. do-loop (like for loop in C)
- ii. do-while loop (like while loop in C)
- iii. do-until loop (like do while loop in C)
i. do-loop (like for loop in C)
The do loop is used for executing a block of code repeatedly for pre-determined number of times. It is also
known as controlled loop. It is considered as the most effective loop in FORTRAN and it is exactly same as
for loop in C programming. The general form of do loop is
do [st_no] [variable = initial_value], [final_value], [step_size]
statement 1
statement 2
… …
statement n
st_no continue
The st_no (statement number) represent the last statement of the body of the loop. The final statement may
be any executable statement but is usually a CONTINUE statement.
Variable may be any type INTEGER, REAL OR DOUBLE PRECISION which is initialized with initial_value, from
which the loop execution begins.
The initial value is increased by step_size and it is increased/decreased until it reaches to final_value.
When the loop-control reaches to final_value, the control is transferred to statement number specified by
the st_no.
Here, step_size and st_no are optional and default value for step_size is 1. The statement enddo can be used
to determine the end of do loop instead of using st_no and continue statement.
ii. do-while loop (like while loop in C)
The do while loop cycles through statements as long as a given condition is true.
do while(i .lt. 5)
i = i + 1
write(*,*)i
end do
However, FORTRAN does not have a formal do-while loop structure. Even though this syntax of do-while loop is
accepted by many compilers, it is not ANSI FORTRAN 77 standard.
iii. do-until loop (like do while loop in C)
If the termination criteria is at the end instead of the beginning of the loop, it is often called an
until-loop. In until loop, the body of the loop is executed first without testing condition. At the end of
the loop, test condition is evaluated.
do
write(*,*)i
i = i + 1
until(i . lt. 5)
However, FORTRAN does not have a formal until loop structure. Even though this syntax of until loop is
accepted by many compilers, it is not ANSI FORTRAN 77 standard.
Array
An array is a group of related data items that share a common name. In other words, an array is data
structure that store a number of data items as a single entity (object). The individual data items are
called elements and all of them have same data types. We have learned different array processing in C. The
theoretical concept is same to that. Here, we study how to manipulate arrays using FORTRAN syntax.
One dimensional array
The general form of one dimensional array is
Syntax: data_type array_name(array_length)
Example: integer num (5)
Here, array_name is the name of the array and the array_length is the number of elements in the array. Array
indexing in FORTRAN starts from one and ends with array_length unlike C.
Implied Do loop
An implied do-loop is a special form of a loop that can be performed on one line. This can be useful for
accessing elements in an array. For example, assuming i is declared as an integer, the following code,
write (*,*) (a(i), i=1,5)
Comparison between normal and implied do loop structure |
Normal do loop |
Implied do loop structure |
purpose |
do 1 k=1, 5, 1
read(*,*) a(k)
1 continue
|
read(*,*) (a(i),i=1,5) |
To read data for array
members a(1), a(2),
a(3), a(4), and a(5)
|
do 2 k=1, 5, 1
write(*,*) a(k)
2 continue
|
write(*,*) (a(i),i=1,5) |
To display content of
array members a(1),
a(2), a(3), a(4), and a(5)
|
Two dimensional array
Two dimensional array is array with two subscripts. The two dimensional array is defined as
Syntax: datatype array_name(dimension1, dimension2, ... , ...)
Example: integer a(3,3)