Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

FORTRAN

Prepared by:
Bal Krishna Shah
Contents
• Character set
• Data types, Constants and variables
• Arithmetic operations, Library Functions
• Structure of Fortran program
• Formatted and Unformatted lnput/Output Statements
• Control Structures: Goto, Logical lF, Arithmetic lF, Do loops
• Arrays; one dimensional and two dimensional
ForTran Introduction
• ForTran is supposed to be the oldest high-level programming
language.
• ForTran stands for “Formula Translator”
• It is used for numeric and scientific computing.
• Fortran was created by a team, led by John Backus at IBM in 1957.
• It was developed in several stages and declared as Fortran 77 in 1977
• There are many versions of the originally developed Fortran
programming language.
Fortran Introduction
• Originally developed versions, Fortran I, II and III are outdated now.
• Fortran IV and 66 are the oldest versions still being used and Fortran 77, 90 and
95 are the commonly used at the time and are also knows as Modern Fortran.

• Later many versions were developed in order to mitigate the lack of features and
issues introduced by the original version developed for scientific calculations
which had very limited support for character strings and other structures
needed for general purpose programming.
Features of Fortran:
• Fortran ruled the programming area for a long time and became very
popular for high performance computing, because it supports −
• Numerical analysis and scientific computation
• Structured programming
• Array programming
• Generic programming
• High performance computing on supercomputers
• Object oriented programming
• Concurrent programming
• Reasonable degree of portability between computer systems
Program Structure
• Every FORTRAN program starts with the keyword PROGRAM followed
by the name of the program and ends with END PROGRAM followed by
the name of program.
• Most modern FORTRAN programs follow this with IMPLICIT none which
tells the compiler that no implicit types are used by the program.
• If it is not set, FORTRAN with attempt to define any variable with the
names I, j, k, l, m and n as integers and others as real.
• The ! Character indicates a comment.
• FORTRAN allows both uppercase and lowercase letters as it is case-
insensitive, except for string literals.
Program structure
Program structure
• Try out the program below:
General rules of FORTRAN
• A line can be up to 132 character long (including spaces)
• Any line can start with leading spaces to improve layout.
• An & at the end of a line indicates that the statement continues on
the next line.
• If the item to be continued is a character constant or a format
statement, the next line should be start with a second &
• Several statements can appear on a line, separated by semi-colons (;)
Lexical Tokens
• A FORTRAN statement consists of lexical tokens which are the
keywords, names, operators, statement labels, literal constants
(except complex literal constants), delimiter pairs.
• Tokens are made of characters in the basic character set.
• Basic character set
o Letters A……Z and a…..z
o Digits 0…..9
o Underscore (_) character
o Special characters = : + blank - * / () [] , . $ ‘ ! “ % & ; <> ?
Identifiers
• An identifier is a name used to identify a variable, procedure, or any
other user-defined items.
• Rules
• It should not be longer than 31 characters.
• It must be composed of alphanumeric characters ( alphabet, digits 0 to 0 and
underscore (_))
• First character of a name must be a letter.
• Names are case-insensitive.
Data Types
• There re five types:
• Integer type
• Real type
• Complex type
• Logical type
• Character type
Integer Type:
•  Integers are normally stored in 4 bytes of storage space
• This allows integers from
• Examples: 12345, 23, 0, -12345 etc
• Invalid integers are of type:
-3, 4 (commas are not allowed)
87648. (decimal point)
8739739727927927(too large)
Real type
• It stores floating point numbers as 3.0, 6.848, -734.3455, etc
• There are two real types, the default real type and double precision type.
• In double precision, D must be used instead of E to indicate exponent. For
example: 1.23D-02 and 0.0123D0 is the double precision type of 0.0123
• Rules:
• Decimal point is necessary
• Commas not allowed
• Decimal point in mantissa
• No decimal point in exponent
Complex type
• A complex number has two parts: A real part and an imaginary part.
• Two real (4 byte) numbers are stored as a pair and are treated as the
real and imaginary part of the complex number.
• Examples are:
The complex number (4.0, 5.0) is equal to (4+5i)
The complex number (6.0, -11.0) is equal to (6-11i)
Logical type:
• Logical types have a value of either .true or .false
OR .TRUE or .FALSE
Character Type
• The character type stores characters and strings.
• The length of the string cam be specified by the len specifier. By
default length is specified at 1.
• Example:
character (len=15) :: name
name = “Ram Shah”
Constant
• The constants are the fixed values that the program cannot
alter/change during execution. These fixed values are also called
literals.
• Constants can be of any basic data types such as an integer constant,
a floating constant, a character constant, a complex constant or a
string literal.
• There are only two logical constants: .true and .false.
• There are two types of constants:
• Literal constants
• Named constants
Literal constants
• A literal constant have a value but no name.
Named constant
• A named constant has a value as well as a name.
• Named constants should be declared at the beginning of a program of
procedure, just like a variable type declaration, indicating its name
and type.
• Named constants are declared with the parameter attribute.
• Example:
Real, parameter :: pi = 3.1415927

You might also like