Fortan [ Introduction ]

FORTAN

The FORTRAN programming language was conceived in the early 1950s the name produced from the two words FORmula TRANslation. In 1966 the language was standardized and FORTRAN IV was born.
Revision of the language led to FORTRAN 77, the language we use today. The standard for FORTRAN 90 is now available although not yet in widespread use. F77 is a subset of F90.

FORTRAN was designed for scientists and engineers, and has dominated this field. For the past 30 years FORTRAN has been used for such projects as the design of bridges and aeroplane structures, it is used for factory automation control, for storm drainage design, analysis of scientific data and so on.

IMPLICIT Statement

 

The implicit none statement is used to inhibit a very old feature of Fortran that by default treats all variables that start with the letters i, j, k, l, m and n as integers and all other variables as real arguments. Implicit None should always be used. It prevents potential confusion in variable types, and makes detection of typographic errors easier

What does IMPLICIT NONE mean to the computer?
It tells the compiler to ignore the Fortran "Implicit" assumption that variables beginning with a-h or o-z are REAL and those beginning with I-n are INTEGER. As a result the compiler requires you to explicitly define the type for every variable that you use in your program.

A Simple Program in Fortran

Let’s write a program that adds two numbers and prints the result −


program addNumbers

! This simple program adds two numbers
   implicit none

! Type declarations
   real :: a, b, result

! Executable statements
   a = 12.0
   b = 15.0
   result = a + b
   print *, 'The total is ', result

end program addNumbers
 
 

Fortran - Data Types

FORTRAN supports six different data types:


1. Integer :   An integer in Fortran is a whole number; it cannot contain commas or a decimal point. Examples of numbers considered integers by Fortran are
12    ,     -1311     ,     0     ,     +43     ,     123456789 
2.  Real: A real number, or more precisely a single precision real number, is written with a decimal point by Fortran, even when it is a whole number.


3.Double precision

4.Complex

5.Character

6.Logical



Fortran - Variables

  • It cannot be longer than 31 characters.
  • It must be composed of alphanumeric characters (all the letters of the alphabet, and the digits 0 to 9) and underscores (_).
  • First character of a name must be a letter.
  • Names are case-insensitive.

 

Based on the basic types explained in previous chapter, following are the variable types −
Sr.No
1 Integer
It can hold only integer values.
2 Real
It stores the floating point numbers.
3 Complex
It is used for storing complex numbers.
4 Logical
It stores logical Boolean values.
5 Character
It stores characters or strings.


Variables are declared at the beginning of a program (or subprogram) in a type declaration statement

For example

integer :: total   
real :: average 
complex :: cx  
logical :: done 
character(len = 80) :: message ! a string of 80 characters
 
 
If a variable is not explicitly declared, it will be assigned a type based on the default naming convention. This convention states that the variable is:
INTEGER if the first letter is I,J,K,L,M,N (I-N) REAL any other letter

Fortran - Constants

 The constants refer to the fixed values that the program cannot alter during 
its execution.
 
Constants
There are two types of constants −

    Literal constants
    Named constants


A literal constant have a value, but no name. 
A named constant has a value as well as a name.

Named constants are declared with the parameter attribute. For example,
real, parameter :: pi = 3.1415927
 
 

Example

 program gravitationalDisp

! this program calculates vertical motion under gravity 
implicit none  

   ! gravitational acceleration
   real, parameter :: g = 9.81   
   
   ! variable declaration
   real :: s ! displacement   
   real :: t ! time  
   real :: u ! initial speed  
   
   ! assigning values 
   t = 5.0   
   u = 50  
   
   ! displacement   
   s = u * t - g * (t**2) / 2  
   
   ! output 
   print *, "Time = ", t
   print *, 'Displacement = ',s  
   
end program gravitationalDisp
 
 

Fortran - Strings

 The Fortran language can treat characters as single character or contiguous 
strings.The length of the string can be specified by len specifier. If no
length is specified, it is 1. You can refer individual characters 
within a string referring by position; the left most character is at 
position 1. 


Arithmetic Operations


     1.   Addition            +
     2.   Subtraction         -
     3.   Multiplication      *
     4.   Division            /
     5.   Exponentiation      **




FORTRAN: Input/Output (I/O) 

 The simplest form of the I/O statement is the list-directed form which
is represented by: 
 
          READ(*,*) item1, item2, item3...

          WRITE(*,*) item1, item2, item3...
 
The first asterisk (*) means the input comes from the keyboard in  a READ 
statement and goes to the screen in a WRITE statement.
The second asterisk (*) means the computer decides how the I/O elements 
should look based on the TYPE of data in the input/output list.
This is sometimes called "FREE-FORMAT".
 

UNIT NUMBER 

 The UNIT is a number which has an association with a particular device. The UNIT is an INTEGER or INTEGER EXPRESSION, generally between 1-30. 

Standard FORTRAN reserves two UNIT numbers for I/O to user. They are:

     UNIT = 5   for INPUT from the keyboard with the READ statement

     UNIT = 6   for OUTPUT to the screen with the WRITE statement
 
 

 The association of the unit number and filename occurs when the OPEN statement is executed.

OPEN(UNIT=n, FILE='filename', options...)

 

Fortran - Operators

Arithmetic Operators

Following table shows all the arithmetic operators supported by Fortran. Assume variable A holds 5 and variable B holds 3 then −

Operator Description Example
+ Addition Operator, adds two operands. A + B will give 8
- Subtraction Operator, subtracts second operand from the first. A - B will give 2
* Multiplication Operator, multiplies both operands. A * B will give 15
/ Division Operator, divides numerator by de-numerator. A / B will give 1
** Exponentiation Operator, raises one operand to the power of the other. A ** B will give 125

Relational Operators

Following table shows all the relational operators supported by Fortran. Assume variable A holds 10 and variable B holds 20, then −

Operator Equivalent Description Example
== .eq. Checks if the values of two operands are equal or not, if yes then condition becomes true. (A == B) is not true.
/= .ne. Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (A != B) is true.
> .gt. Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true.
< .lt. Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true.
>= .ge. Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true.
<= .le. Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true.

Logical Operators

Logical operators in Fortran work only on logical values .true. and .false.
The following table shows all the logical operators supported by Fortran. Assume variable A holds .true. and variable B holds .false. , then −

Operator Description Example
.and. Called Logical AND operator. If both the operands are non-zero, then condition becomes true. (A .and. B) is false.
.or. Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true. (A .or. B) is true.
.not. Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(A .and. B) is true.
.eqv. Called Logical EQUIVALENT Operator. Used to check equivalence of two logical values. (A .eqv. B) is false.
.neqv. Called Logical NON-EQUIVALENT Operator. Used to check non-equivalence of two logical values. (A .neqv. B) is true.

Comments