Fortran
Fortran is a (computer) language Like any other language it also has very precise grammar For programming language this (grammar) is termed as syntax
Fortran Alphabets
Letters: Upper and lower case letters: ABCDEFGHI J KLM NOPQRSTUVWXYZ abcdefghijklm nopqrst vwxyz Digits: 0123456789 Special Characters: space () *+-/:=_ !&$;<>%? ,. Fortran is not case sensitive
Data Types
Data Types INTEGER REAL DOUBLE PRECISION COMPLEX LOGICAL CHARACTER Characteristics Whole numbers stored exactly Numbers, which may have fractional parts, with limited precision Similar to real but with greater Precision Complex numbers; stored as an ordered pair of real numbers A Boolean value; either true or false A string of characters of fixed length
CONSTANTS
Integer Constants
The general form of an integer constant is:
sign digits
Why?
Real Constants
A real constant must contain a decimal point or an exponent ( or both) The letter E used in Fortran Represent times 10 to the power of. For example, the constant 1.23410-5 is written as 1.234E-5.
sign digits
exponent exponent-section
Decimal Form: 123.45, .123, 123., -0.12, +.12 Exponential Form: 12.34E3, 12.3E+3,-1.2E-3
Incorrect
12,355.67 65
Complex Constants
Complex constants have the form of two real or integer constants separated by comma enclosed in parenthesis For A+iB the constant is (A,B) Examples
(3.14,-5.67) (0,0) (-0.999,2.718E15)
Logical Constants
.FALSE.
Character Constants
A string of characters enclosed between apostrophes or double quotes Examples
John or John or John Dow #2 or John Dow #2
Incorrect:
you and me Techs seminar Have a nice day
VARIABLES
Early programs were written in binary language
Computer instructions had to be hand-encoded into binary numbers Then the binary code was entered into the machine Programmers had to explicitly assign and reference any storage locations
Modern day programming languages allow us to assign symbolic names (known as variable names) for storing program computations and results
Fortran Variables
The type of a FORTRAN variable must be one of the six data types mentioned before. The type of each variables determines the type of value that may be assigned to that variable.
IDENTIFIERS
Identifiers are the names given to programs, constants, variables, and other entities in a program.
Fortran Identifiers
No more than 31 characters The first character must be a letter The remaining, if any, may be letters, digits or underscores
Type Declaration
INTEGER :: ZIP, Mean, Total REAL :: Average, error, sum CHARACTER (LEN=15) :: Name, Street CHARACTER(15) :: Name, Street CHARACTER :: letter, digit CHARACTER (10) :: City, Nation*20, Box, Bug*1
Variable Initialization
Is this initialization?
Examples:
INTEGER, PARAMETER :: Limit =100 REAL, PARAMETER :: PI = 3.14159, TWOPI = 2*PI CHARACTER(4), PARAMETER :: City=LA, LN=Newton CHARACTER(*), PARAMETER :: LastName=Newton
OPERATORS
Type
Arithmetic
Operators
** * + / -
Associativity
Right-to-left Left-to-right Left-to-right
Arithmetic Expressions
The order of evaluation of an expression:
Sub-expressions in parenthesis Function reference Exponentiation, i.e., raising to a power Multiplication and division Addition and subtraction
Arithmetic Expressions
Now lets find the value of the following expression
(5*(11-5)**2**2)*4+9 (5*[(11-5)]**2**2)*4+9 (5*6**2**2)*4+9 (5*6**[2**2])*4+9 (5*6**4)*4+9 (5*[6**4])*4+9 (5*1296)*4+9 [(5*1296)]*4+9 6480*4+9 (6480*4)+9 25920+9 25925
Sub-expressions in parenthesis Function reference Exponentiation (right to left) Multiplication and division (left to right) Addition and subtraction (left to right)
Integer Division
2*4*5/3**2 = ? 4.4444 Integer division results in another integer:
2*4*5/[3**2] 2*4*5/9 [2*4*5]/9 40/9 4
Mixed-mode Operation
Expressions involving different types of numeric operands: real and integer
Operation INTEGER REAL Conversion REALREAL Result REAL
3.0+8/5 = 4.0
3.0 + 8/5 3.0+[8/5] 3.0+1 3.0+{1} 3.0+1.0 4.0
5.0*3/4 = ?
5.0*3/4
3/4*5.0
{3/4}*5.0 0*5.0 0
5.0*3/4
5.0*{3}/4 5.0*3.0/4 [5.0*3.0]/4 15.0/4 15.0/{4} 15.0/4.0 3.75
COMMON FUNCTIONS
ABS (x) SQRT (x) SIN (x) COS (x) TAN (x) ASIN (x) ACOS (x) ATAN (x) EXP (x) LOG (x) LOG10(x) :absolute value of x :square root of x :sine of x radians :cosine of x radians :tangent of x radians :arc sine of x :arc cosine of x :arc tangent of x :ex :ln(x) :log10(x)
MAX (x1, ..,xn) :maximum of x1, , xn MIN (x1, ..,xn) :minimum of x1, , xn MOD(x,y) :x INT(x/y)*y
Syntax:
variable = expression
Replacement Statement
Input/Output
Fortran provides two types of input /output statements:
Formatted input/output List-directed input/output
List-directed output PRINT*, output-list or WRITE (*,*) output-list List-directed input READ*, input-list or READ (*,*) input-list
PROGRAM program-name IMPLICIT NONE specification/declaration part execution part END PROGRAM program-name
Program that inputs from user, two integers and adds them; then displays the result Program that illustrate the use of various arithmetic operators Program that uses intrinsic functions
sumof_ij
arith_oper
use_of_func
PROGRAM first IMPLICIT NONE PRINT*, Programming is fun PRINT*, And programming in FORTRAN is more fun END PROGRAM first
PROGRAM arith_exp IMPLICIT NONE INTEGER :: a=100, b=2, c=25, d=4, Result Result = a b PRINT*, ab = , result Result = b*c PRINT*, b*c = , result Result = a/c PRINT*, a/c = , result Result = a**d PRINT*, a**d = , result END PROGRAM arith_exp
PROGRAM sumof_ij PROGRAM IMPLICIT NONE Non executable portion INTEGER :: i, j, sumij READ*, i, j Executable portion sumij = i+j PRINT*, sumij END PROGRAM END PROGRAM sumof_ij
PROGRAM use_of_func IMPLICIT NONE INTEGER :: a=100, b=90 , c=17, d =4, Result REAL :: Rresult Result=MAX(a,b,c,d) PRINT*, Maximum = , result Result=MIN(a,b,c,d) PRINT*, Minimum = , result RResult=SIN(REAL(b)) PRINT*, sinb = , Rresult RResult=SQRT(REAL(a)) PRINT*, Square root of a = , Rresult END PROGRAM use_of_func