Conditional
Construct
Program Examples
Quadratic
Equation
•
finding roots of a quadratic equation
ax2 + bx + c = 0
•
can have
–
exactly one root
–
two distinct real roots
–
two complex conjugate roots
•
type of roots depend on the discriminant
b2-4ac
Quadratic
Equation
program quadratic
implicit none
real :: a, b, c, disc, x_r, x_r1, x_r2, x_im1, x_im2
real, parameter :: eps = 1.0e-6
print *, “type values of a, b, c”
read *, a, b, c
if
(a == 0.0) then
! a is 0, not a quadratic equation
print *, “equation is not quadratic”
else
disc = b*b – 4.0*a*c
x_r = -b/(2.0*a)
if
( abs(disc) < eps ) then
! discriminant is nearly zero
print *, “double real root”, x_r
elseif ( disc > 0 ) then
! two distinct real roots
disc = sqrt(disc)/(2.0*a)
x_r1 = x_r + disc ! disc used as temporary
variable
x_r2 = x_r - disc
print *, “two real roots”, x_r1, “
and”, x_r2
else
!
disc is negative, complex conjugate roots
x_im1 = sqrt(-disc)/(2.0*a)
x_im2 = - x_im1
print *, “complex conjugate roots”, x_r, “+”, &
x_im1, “i
and”, x_r, “-”, x_im2, “i”
endif
endif
end program quadratic
Comparing
Real Numbers
•
instead of disc == 0.0
we have checked abs(disc) < eps as condition for double root
•
misleading results occur otherwise
0.1x2 - 0.3 x + 0.225 = 0
•
has 1.5 as double root
•
errors in representation give disc > 0.0
•
roots obtained are 1.5004526 and 1.4995474
Comparing
Real Numbers
•
double roots undesirable in many applications
•
two roots ‘close’ to each other may be treated as
double
•
if abs(disc) is ‘small’ roots are close
•
a parameter eps (epsilon) is usually used for comparing
reals
•
two reals are treated as ‘equal’ if absolute value of
difference is < eps
Quadratic
Equations
•
numerical problems in solving quadratic equations by
this method
•
if two roots differ by orders of magnitude, smaller
root cannot be found accurately
x2 – 1000.001x + 1.0 = 0
•
two real roots
1.0000000e+03 and 1.0070801e-03
•
what happens if actual roots are 1.0e+4 and 1.0e-4 ( run program and see)
Case
Construct
•
simplified form of the if - then - elseif
- else construct
•
useful when all conditions depend on the value of the
same variable/expression
•
different statements executed based on the value of the
variable/expression
•
variable/expression must be of integer type
–
character type is also allowed( see later)
•
typically used with integer/character “codes”
Case
Construct
select case(i) ! i may be an integer expression
case(list_of_values1)
statements1
case(list_of_values2)
statements2
case default
statements3
end select
Case
Construct
•
value of variable/expression is compared with the
list_of_values in each case statement
•
the statements after the case statement which matches
value of variable are executed till the next case or end select
statement
•
if value does not match any case, statements
following case default are executed
•
case default is optional but should be last
•
statement after end select is executed next
Case Construct
•
ranges of values may be given
•
a range is specified as
–
(i : j), all values k such that i <= k <=j
–
(: j) , all k <= j
–
(i :) , all k >= i
• i,
j must be integer constants
• ranges
and values for different cases must be disjoint
Example
of Case Construct
• days in a month
select case(month)
! month is an integer variable
case(1,3,5,7,8,10,12)
! if value of month is either 1,3,5,7,8,10
or 12 then
days = 31
case(4,6,9,11)
! elseif value is either 4,6,9,11
days = 30
case(2)
! elseif month has value 2
if (leap_year) then ! leap_year is a logical variable
days = 29
else
days = 28
endif
case default ! else
print * , “error in month”
end select
Using
Integer Codes
•
many times a program is required to perform multiple
tasks
•
choice of task is left to the user
•
a “menu” of possible tasks is given to user
•
each task is “encoded” by an integer
•
user inputs an integer to decide the task
•
further choice of subtasks within a task
•
case constructs can also be nested
Example
•
a simple example
•
two integers are read
•
either an arithmetic or relational operation is
performed on them
•
there is a further choice of type of arithmetic or
relational operation
•
assign integer codes to each operation
•
user determines the operation to be performed
Example
program illustrate_case
implicit none
integer :: n, m, type_of_op, op
print *, “type values of n,m”
read *, n, m
print *, “enter type of operation”
print *, “1 for arithmetic”
print *, “2 for relational”
read *, type_of_op
select case(type_of_op)
case(1) ! arithmetic operation
print *, “enter operation to be performed”
print *, “1 for addition”
print *, “2 for multiplication”
read *, op
select case(op)
case(1)
print *, n+m
case(2)
print *, n*m
case default
print *, “invalid operation”
end select
case(2)
print *, “enter operation to be performed”
print *, “1 for checking n == m”
print *, “2 for checking n <= m”
read *, op
select case(op)
case(1)
print *, n == m
case(2)
print *, n <= m
case default
print *, “invalid operation”
end select
case default
print *, “invalid type of operation”
end select
end program illustrate_case
! test it out
Nesting
of Case
•
select case construct may also be nested
• a
select case must be completely contained within one case of outer
select case
•
case statement applies to innermost select
case for which end select comes later
•
different constructs may be nested inside each other
•
a construct nested inside another must end before the
outer construct
Summary
•
some uses of if – then – elseif – else
•
quadratic equations
–
three different possibilities
–
checked using appropriate conditions
•
comparison of reals
–
== should not be used with reals
•
case construct
•
using case to perform multiple tasks