M.Sc 1st year
Declaration of variables:
We need to tell the computer that the data we are
entering is of real or integer or character type otherwise there may be errors.
For example by default the variables which begin by
letters i to n are considered as integer variable and all other are considered as single precision real variable.
To solve the problem we use the type statement as
type :: vname1, vname2....
Where type is one of data type we want to use, e.g. Integer, real or character
Suppose we are using i,j,k as real then we write as
Real :: i,j,k
For integer we write
Integer:: i,j,k
Note that the above statements are written in the
Example :
To avoid the errors always use
implicit none
Keep Constant consistent in the program :
We should declare constants in the beginning using Parameter attribute
type : means whether it is real, integer, character name: means name assigned to constant value
Assignment statements and arithmetic calculations:
General form of assignment statement is:
Above statement calculate the value of expression written on the right side and store that value in the variable name written on the left side. Equal sign is called assignment operator.
Consider expression:
Operators:
These helps us in performing different computational operations.
Arithmetic operators(+, -, *, /, **)
Relational operators(.eq., .ge., .lt., .le., .ne., .eq.)
Precedence and Associativity of operators:
Precedence determine the order in which different operators are evaluated.
Associativity determine the order in which the
operators of same precedence are evaluated.
Associativity can be from left to right or right to left
2+5+7
Arithmetic operators:
3 3 2 2 1
The five arithmetic operators described above are binary operators means they occur between two operand e.g. a+b, c*d etc
+ and – are also unary operators
e.g. +223, -232
Rules for using arithmetic operators:
•No two operators occur side by side
e.g. 2*-4 is not write. We should use 2*(-4)
•Implied multiplication is illegal in fortran e.g. We cannot write x(y+z)
We should wrtie x*(y+z)
Integer Arithmetic: It involve the calculations among integers and always produce integer results.
Be careful while using integers for division. If division of two
integers gives us fractional part then it automatically neglect fractional part.
e.g 9/4 = 2, ¾ = 0 , ½ = 0 etc.
Real Arithmetic: Calculations using reals always give
results in real.
But due to precise nature some time theoretical and calculations using computers gives different results
Hierarchy of operations:
Suppose we have expression for distance:
How to evaluate: If exponentiation is evaluated first, then Expression will look as
Mixed Mode Expression:
It involve the expressions where we have both real and integer
It is advisable to use real instead of integers to avoid errors in calculations
Even though in the following expression we expect 3.25 but actually we get the result 3 because the nres in which result has to be stored is an integer.
Mixed type integer and exponentiation:
As said earlier the mixed mode expression are generally not desirable
But when expressions involve exponentiation then it is
more good to use mixed mode expressions Consider,
Where y, result are real and n is integer
It mean y is multiplied n times with itself
But suppose we have
where x, y, result are real.
Now let x is suppose 2.5 then it is meaningless to say
that y is multiplied with itself 2.5 times
To evaluate above expression we shall use
But here also y cannot be negative because log of –ve is undefined. We shall get run time error.
All relation operators are binary operators and appear
between their operands there is no relative precedence between the relational operators.
In relational operators when real and Integers are
When we compare two characters the these are evaluated in alphabetical order
Numeric values cannot be compared with
Character. You will get an error.
If there are arithmetic and relation
operators in an expression the first arithmetic
Combinational Logical Operators:
Hierarchy of operations:
As discussed earlier first we evaluate the arithmetic operators
Relation operators are evaluated working from left to
right
.NOT., operators are evaluated working from left to right
.AND., left to right
OR, left to right