Download as pdf or txt
Download as pdf or txt
You are on page 1of 45

Unit – 11

FORTRAN
Computer Programming

Prepared By
Er. Rama Bastola
Unit 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

2
FORTRAN: Introduction [1]
• Fortran, as derived from FORmula TRANslator, is a general-purpose,
imperative programming language.
• It is used for numeric and scientific computing.
• Fortran was created by a team, led by John Backus at IBM in 1957.
• Initially the name used to be written in all capital, but current
standards and implementations only require the first letter to be
capital.

3
FORTRAN: Introduction [2]
• Originally developed for scientific calculations, it had very limited
support for character strings and other structures needed for general
purpose programming.
• Later extensions and developments made it into a high level
programming language with good degree of portability.
• Original versions, Fortran I, II and III are considered obsolete now.
• Oldest version still in use is Fortran IV, and Fortran 66.
• Most commonly used versions today are : Fortran 77, Fortran 90, and Fortran
95.
• Fortran 77 added strings as a distinct type.
• Fortran 90 added various sorts of threading, and direct array processing.

4
FORTRAN: Introduction [3]
• 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

5
Program Structure [1]
• All Fortran programs start with the keyword program and end with
the keyword end program, followed by the name of the program.
• The implicit none statement allows the compiler to check that all
your variable types are declared properly.
• You must always use implicit none at the start of every program.
• Comments in Fortran are started with the exclamation mark
• Indentation of code lines is a good practice for keeping a program
readable.
• Fortran allows both uppercase and lowercase letters. Fortran is case-
insensitive, except for string literals.

6
Program Structure [2]

7
General Rules
• 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 (;)

8
Fortran Tokens
• Tokens are made of characters in the basic character set.
• A token could be a keyword, an identifier, a constant, a string literal,
or a symbol.
• Program statements are made of tokens.

• Basic Character Set


Letters A ... Z and a ... z
Digits 0 ... 9
Underscore (_) character
Special characters = : + blank - * / ( ) [ ] , . $ ' ! " % & ; < > ?
9
Identifiers
• An identifier is a name used to identify a variable, procedure, or any
other user-defined item.
• Rules :
• 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

10
Keywords
• Keywords are special words, reserved for the language. These
reserved words cannot be used as identifiers

11
The non-I/O
keywords

12
Data Types
• Five Types
• Integer type
• Real type
• Complex type
• Logical type
• Character type

13
Integer Type
• Integers are normally stored in 4 bytes (32 bits) of storage space.
• This allows integers from -2147483647 (231) to +2147483647 (231 -1)
to be represented.
• For example : 1245, 0, -9874 etc

• Invalid Integers

14
Real Type
• It stores the floating point numbers, such as 5.0, 8.2354, -582.123, etc.
• Traditionally there are two different real types, the default real type
and double precision type.
• Illegal real type:

• In double precision, D must be used instead of E to indicate exponent. For


example, 1.23D-02 and 0.0123D0 represent the double precision version of
0.0123

15
Complex Type
• A complex number has two parts, the real part and the imaginary
part.
• Two real (4byte) numbers stored as a pair and treated as the real and
imaginary part of a complex number.
• For example, the complex number (5.0, -10.0) is equal to 5.0 – 10.0i

16
Logical Type
• Logical variables have a value of either .true. or .false. (or .TRUE. or
.FALSE.)

17
Character Type
• The character type stores characters and strings.
• The length of the string can be specified by len specifier. If no length
is specified, it is 1.
• For Example :
character (len=20) :: name
name = “ Roshan Sharma”

18
Implicit Type
• Older versions of Fortran allowed a feature called implicit typing, 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, or 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
19
Constant
• The constants refer to the fixed values that the program cannot alter
during its execution. These fixed values are also called literals.
• Constants can be of any of the basic data types like 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.
• The constants are treated just like regular variables, except that their
values cannot be modified after their definition.
• There are two types of constants −
• Literal constants
• Named constants

20
Constant : Literal Constant
• A literal constant have a value, but no name

21
Constant : Name Constant
• A named constant has a value as well as a name.
• Named constants should be declared at the beginning of a program or
procedure, just like a variable type declaration, indicating its name
and type.
• Named constants are declared with the parameter attribute.
• For example
real, parameter :: pi = 3.1415927

22
Operators

• An operator is a symbol that tells the compiler to perform specific


mathematical or logical manipulations.
• Fortran provides the following types of operators −
• Arithmetic Operators
• Relational Operators
• Logical Operators

23
Arithmetic Operators
For A=5 and B=3

24
Relational
Operators
For A=10 and B=20

25
Logical Operators
• Logical operators in Fortran work only on logical values .true. and .false.
• Assume variable A holds .true. and variable B holds .false. , then

26
Operators Precedence in Fortran
• Here, operators with the highest precedence appear at the top of the table, those
with the lowest appear at the bottom.
• Within an expression, higher precedence operators will be evaluated first

27
Example:

28
Fortran : Input/Output [Free-Format / Formatted]
• Data can be read from keyboard using read * statement and display output
to the screen using the print* statement, respectively.
• This form of input-output is free format I/O, and it is called list-
directed input-output

• 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".
29
Formatted Input Output [1]
• Syntax:

• Where,
• fmt is the format specification
• variable-list is a list of the variables to be read from keyboard or written on screen
• Format specification defines the way in which formatted data is displayed.
• It consists of a string, containing a list of edit or format descriptors in
parentheses.
• An edit descriptor specifies the exact format, for example, width, digits
after decimal point etc., in which characters and numbers are displayed.

30
Formatted Input Output [2]

31
Formatted Input Output : Example 1

32
Formatted Input Output : Example 2

33
Fortran - Decisions: If-then
• An if… then statement consists of a logical expression followed by one
or more statements and terminated by an end if statement
• Syntax:

• Name to the if block can be given as shown in above syntax


34
Fortran - Decisions: If-then : Example

35
Fortran - Decisions: If-then-else : Syntax

36
Fortran - Decisions: If-then-else : Example

37
Fortran - Decisions: if-else if-else:Syntax

38
Fortran - Decisions: if-else if-else: Example

39
Fortran - Decisions: Nested if: Syntax

40
Fortran - Decisions: select case: Syntax

41
Fortran - Decisions: select case: Example

42
Arithmetic IF (Obsolescent Feature)
• An Arithmetic IF statement has the form:

IF(arithmetic expression) label1, label2, label3

Where any valid arithmetic expression is tested and control passes to label1,
label2 or label3, according as the value of the expression is negative, zero or
positive respectively

43
Arithmetic IF : Example

44
Next
• Do loops
• Arrays; one dimensional and two dimensional
• Modules, Procedures

You might also like