Basic
Programming Concepts
Assignments and Expressions
Assignment
Statement
is
the simplest executable statement
assigns
a value to a declared variable
integer :: i
real :: x
i = 251
x = 3.7
251
is stored in the location represented by i
i
is assigned value 251
Assignment
Statement
value
assigned to a variable may be computed from the values of other variables
the
variables used in the computation must have already been assigned values
value
of a variable is undefined until it has been assigned one
using
variables with undefined values may give unpredictable results
Assignment
Statement
real :: force, mass, acceleration
mass = 2.0
variable
mass assigned value 2.0
acceleration = 1.4
force = mass * acceleration
this
statement multiplies the values of the variables mass and acceleration and
assigns the result (2.8) to the variable force
Assignments
and Equations
assignments
are different from equations
force
= mass * acceleration is an equation relating the 3 variables, universally true
force
= mass * acceleration is also an assignment statement
indicates
how the value of force is computed from the values of mass and acceleration
force
may be assigned other values later
Assignments
and Equations
(a+b)*c
= a*c + b*c is a valid equation
(a+b)*c
= a*c + b*c has no meaning as an assignment statement
the left hand
side of an assignment statement must be a variable
the right hand side of an assignment can be any expression
in
an equation both sides can be expressions
Assignments
and Equations
i
= i + 1 is a meaningless equation
i = i + 1 is a perfectly valid assignment statement
the
new value of variable i is the old value incremented by 1
= in Fortran
stands for assignment
value of an
expression is assigned to a variable
Expressions
general
form of assignment statement
variable = expression
expressions are built from variables and constants
using operators
expressions also have a value and type
both
sides of an assignment statement must have the same type
value
of the expression is assigned to the variable by the assignment statement
Arithmetic
Expressions
arithmetic
expressions are built from integer and real variables and constants
arithmetic
operators are used to combine variables and constants
+, -, *, /, ** are the allowed arithmetic operators in
Fortran
** is the
exponentiation operator
real
exponents with negative operands can give unpredictable results
Arithmetic
Expressions
integer,
real variables and constants are expressions
if
e and f are expressions then so are
± e,
e + f, e f, e * f,
e / f, ( e )
value (e +
f) = value (e) + value (f)
type of e +
f is integer if both e and f are integer, constant if both are constants
similarly
for other operators
two
operator symbols cannot occur together
Examples
of Expressions
i
+ j / k ** 2 * ( 3 - m)
i,
j, k, m are integer variables
2,
3 are integer constants
the
type of this expression is integer
all
variables and constants in the expression are of integer type
result is
also of integer type
Examples
of Expressions
x
+ y / z + 2.5 * p 5.2e-1
x,
y, z, p are real variables
2.5
and 5.2e-1 = 0.52 are real constants
the
type of this expression and that of the result is real
x
= x + y / z + 2.5 * p 5.2e-1
is an assignment statement
Operator
Precedence
order
of performing operations is important
a
+ b * c can be interpreted as
( a + b) *
c or
a + ( b * c)
operators are assigned a precedence
higher
precedence operators executed first
equal
precedence operators are executed from left to right ( except for **)
Operator
Precedence
expression
in innermost parenthesis first
exponentiation
( right to left)
multiplication
and division
done left to
right
unary
plus and minus (sign)
addition
and subtraction
done left to
right
parenthesis
used to change the order
Operator
Precedence
i
+ j / k ** 2 * (3 - m)
expression
in brackets evaluated first (3-m)
exponentiation
next (k ** 2)
division,
multiplication ((j / (k**2)) * (3-m))
additions
and subtractions
actual
expression
(i +
(( j / ( k ** 2) ) *
(3-m)))
put brackets to clarify
evaluation order
redundant brackets do not harm
Integer
Division
integer
division results in integer values
4/5
has value 0 and not 0.8
4
and 5 are integer constants and 4/5 is an integer expression with integer
result
if
0.8 is the required answer it should be written as 4.0/5.0
integer
division ignores the fractional part
-5/4
= 5/(- 4) = -1
Mixing
Integers and Reals
using
integers and reals in one expression
the
type of such an expression is real
part
of the expression may be integer
if
e is an integer expression and f a real expression, e + f is real
e
is evaluated and the value converted to real form before adding to the value of
f
converting
an integer to real may lose accuracy, source of error
Mixing
Integers and Reals
possible
to assign a real expression to an integer variable and vice versa
fractional
part of the value is ignored
can
lead to overflow if the real value is larger than the largest possible integer
mixing
of integers and reals should be avoided in general
intrinsic
functions are available for type conversion and should be used
Intrinsic
Functions
Fortran
provides many built-in functions which can be used in expressions
intrinsic
functions are available for conversion from reals to integers and vice-versa
function_name(list_of_arguments)
functions
have highest precedence
arguments
evaluated before function
conversion
is done under users control
errors
in conversion are easier to detect
Intrinsic
Functions
real(e)
value of integer
expression e changed to real form
real(e) is an expression of type real
int(e)
value of real expression e changed to integer
fractional part is ignored
nint(e)
same as int except that
real is converted to the nearest integer
Intrinsic
Functions
floor(e)
value of real expression
e changed to integer
largest integer <=
value of e
ceiling(e)
real converted to
smallest integer >= value of e
anint(e)
real converted to
nearest integer in real form
same as real(nint(e))
Summary
assignment
statement used to assign values to variables
expressions
used to compute values
arithmetic
expressions for numbers
arithmetic
operators have a precedence which defines the order of evaluation
intrinsic
functions are available to change the type and value of expressions