Control
Constructs
Conditional Construct
Conditional
Construct
assignment
statements are executed in the order in which they are written
sometimes
we may want this order to depend on the values of variables
statements
to be executed may depend on values of variables
conditional
construct allows this
a
construct is a group of statements
Examples
incorrect
input
should be
checked and appropriate message given if it is incorrect
distance
between point and line
line given
by ax + by + c = 0
both a and b
should not be 0.0
earlier
program will fail as a division by 0.0 would occur
Examples
special
cases
input may be
correct but of a special type
may have to
be treated differently
intersection
of two lines
lines may be
parallel or identical
division by
0.0 would occur in the program
input should
be checked for these cases and appropriate message given
Examples
complicated
relationships between variables
marks
and grade_points in a course
not a simple
arithmetic relation
cannot be
expressed by a formula
is a step
function with steps of different lengths
number
of days in a month
Conditional
Construct
general
form of conditional construct
if ( condition1 ) then
statements1
elseif ( condition2 ) then
statements2
else
statements3
endif
Conditional
Construct
if ( condition1 ) then
statements1
statements1
can be any sequence of statements
these
statements are executed only if condition1 is true
condition1 is a logical expression whose value
is either .true. or .false.
statements2
and 3 are not executed if condition1 is true
Conditional
Construct
elseif ( condition2 ) then
statements2
statements2 are executed only if condition1 is .false.
and condition2 is .true.
statements1 and 3 are not executed in this case
any
number of such elseif statements with different conditions may be used
Conditional
Construct
else statements3
statements3 are executed only if all conditions checked
in if and elseif are .false.
endif
indicates the end of if construct
exactly
one of the 3 groups of statements is executed
statement
following endif is executed after it
elseif and else statements are optional
Conditional
Construct
Forms
of Conditional Construct
if (condition) then statement1
else statement2 endif
if (condition1) then statement1
elseif (condition2) then statement2 endif
if (condition) then statement1 endif
if (condition) statement
a single statement without any endif
Example
distance
between point and line
if ( abs(a) + abs(b) == 0.0 ) then
print * , input data is incorrect
print * , line is not specified
else
! insert original program (executable part) here
endif
Example
can
be also written as
if ( abs(a) + abs(b) == 0.0 ) then
print * , input data is incorrect
print * , line is not specified
stop ! program execution stops
endif
! insert original program (executable part) here
Example
parallel and identical
lines
if ( a*q - b*p == 0.0) then
if
( a*r c*p == 0.0) then
print *, lines are identical
else
print *, lines are parallel
endif
stop
endif
Nesting
of Constructs
an
if construct can occur inside another
it should be completely within the if, elseif
or else part of the outer if
nesting
of if construct
indentation
used to show nesting level
if
and endif statements paired like brackets
elseif
and else statements apply to the innermost if for which endif
comes later
nesting
may be up to 20 levels deep
Nesting
of Constructs
if then
if then
else
endif
elseif
if then
if then
endif
endif
endif
Logical
Variables
declare
variables to be of logical type
logical :: good, non_zero
possible values are .true. or .false.
can
be used in conditional construct
if
( good ) then
if
( non_zero) then
endif
endif
Logical
Expressions
logical
variables assigned values using logical expressions
logical
expressions can use logical variables and constants with logical operators
use
arithmetic expressions with relational operators
have
the same structure as arithmetic expressions
Relational
Operators
used
to compare numerical values
if
e and f are arithmetic expressions of the same type ( integer or real)
e < f, e <= f, e > f, e >= f, e == f, e /= f
are logical expressions
e
< f has value .true. if and
only if value of e is strictly less than value of f
similarly
for other relational operators
Logical
Operators
logical
expressions combined using logical operators
if
p and q are logical expressions
.not. p, p .and. q, p
.or. q, p .eqv. q,
p .neqv. q are logical expressions
.not. p has value .true. if and only if p
has value .false.
p
.eqv. q is true if and only if p
and q have the same value
Operator
Precedence
a
complex logical expression may include arithmetic, relational and logical operators
operator
precedence is defined to specify order of evaluation
arithmetic
operators are evaluated first followed by relational operators
logical
operators are evaluated last
precedence
amongst logical operators
.not. , .and. , .or.
, .eqv. and .neqv.
Example
.not. 3 <= 5/2 .and. 2+5 > 3*2
arithmetic
first ( first * and / then +)
.not. 3 <= 2 .and. 7 > 6
relational
operators next
.not. .false. .and. .true.
.not.
evaluated next
.true. .and. .true.
value is .true.
Summary
conditional
construct used to execute different statements based on values of variables
general
form
if ( logical_expression1) then
statements1
elseif ( logical_expression2) then
statements2
else
statements3
endif
Summary
logical
expressions are built from logical variables and constants using logical
operators
logical
expressions may also contain arithmetic expressions with a relational operator
operator
precedence defines order of evaluation