Title: IF
1Chapter 3
2Control Constructs Branches
- Definitions
- Code statements or expressions in a program
- Block a group of codes
- Branching selecting or skipping certain blocks
in a program - Branching can be done using
- IF Statement
- SELECT CASE
3IF.. statement
- IF construct
- A block of code is executed if-and-only-if a
certain logical expression is true. - IF (logical_expr) THEN
- Statement 1
- Statement 2
-
- END IF
- If the logical_expression is true, the program
executes the block of codes between IF END IF
4IF.. statement
IF (logical_expr) THEN Statement 1 Statement
2 END IF
5IF.. statement
- Example Solving quadratic equation
- PROGRAM QUADRATIC
- IMPLICIT NONE
-
- IF (b2 4ac lt 0) THEN
- WRITE (,) There are two complex roots to
this equation. - END IF
-
- END PROGRAM
6IF.. statement
- Example Solving quadratic equation
- PROGRAM QUADRATIC
- IMPLICIT NONE
-
- IF (b2 4ac lt 0) THEN
- WRITE (,) There are two complex roots to
this equation. - END IF
-
- END PROGRAM
7IF, ELSE IF and ELSE
- IF if true then execute, if false then skip
- What if we had other situations?
- Use ELSE IF ELSE
- IF (logical_expr1) THEN
- Statement 1
- Statement 2
-
- ELSE IF (logical_expr2) THEN
- Statement 1
- Statement 2
-
- ELSE
- Statement 1
- Statement 2
-
- END IF
8IF, ELSE IF and ELSE
9IF, ELSE IF and ELSE
- Example
- IF (b2-4ac lt 0.0) THEN
- WRITE(,) This equation has complex roots.
- ELSE IF (b2-4ac gt 0.0) THEN
- WRITE(,) This equation has two distinct real
roots. - ELSE
- WRITE(,) This equation has two identical
real roots. - END IF
10Example 3-2 (The Quadratic Equation)
- Problem
- Design and write a program to solve for the roots
of a quadratic equation regardless of type.
- Reminder (Design Procedure)
- Problem statement
- Defining inputs and outputs
- Algorithm design
- Task ? subtasks
- pseudo code or/and flowchart
- Turn algorithm into Fortran statements
- Test
11Example 3-2 (The Quadratic Equation)
- Problem
- Design and write a program to solve for the roots
of a quadratic equation regardless of type.
- Problem statement (make it more clear)
- Design and write a program that calculates the
two roots of the quadratic equation a.x2b.xc0.
Depending on the three coefficients entered by
the user the roots might be real or complex.
Furthermore, the two real roots might be distinct
or identical. The program should solve for all of
these conditions and display the two roots and
clearly state the type of the roots.
12Example 3-2 (The Quadratic Equation)
- Defining inputs and outputs
- Inputs
- Equation coefficients (real)
- a, b, c
- Outputs
- Roots (real) and statement that describe their
type - x1, x2 (complex, real distinct or identical)
13Example 3-2 (The Quadratic Equation)
- Design Algorithm
- Main tasks
- Read the input data (a,b,c)
- Calculate the roots
- Write the roots (x1, x2)
14Example 3-2 (The Quadratic Equation)
- Design Algorithm
- Main tasks
- Read the input data (a,b,c)
- Calculate the roots
- Write the roots (x1, x2)
Ask user to enter coefficients Read coefficient
a, b, c Calculate discriminant disc ? b2 - 4
a c IF disc gt 0 THEN Calculate two distinct
real roots Write the distinct real roots ELSE IF
disc lt 0 THEN Calculate complex roots Write the
two complex roots ELSE Calculate one real
root Write the repeated root END IF
15Ask user to enter coefficients Read coefficient
a, b, c Calculate discriminant disc ? b2 - 4 a
c IF disc gt 0 THEN x1 ? (-bsqrt(disc))/(2.
a) x2 ? (-b-sqrt(disc))/(2. a) Write message
that equation has two distinct real roots Write
x1 and x2 ELSE IF disc lt 0 THEN real_part ?
-b/(2. a) Imag_part ? sqrt(abs(disc))/(2.
a) Write message that equation has two complex
roots Write the two complex roots ELSE x1 ?
-b/(2. a) Write message that equation has two
identical real roots Write x1 END IF
Ask user to enter coefficients Read coefficient
a, b, c Calculate discriminant disc ? b2 - 4
a c IF disc gt 0 THEN Calculate two distinct
real roots Write the distinct real roots ELSE IF
disc lt 0 THEN Calculate complex roots Write the
two complex roots ELSE Calculate one real
root Write the repeated root END IF
16Ask user to enter coefficients Read coefficient
a, b, c Calculate discriminant disc ? b2 - 4 a
c IF disc gt 0 THEN x1 ? (-bsqrt(disc))/(2.
a) x2 ? (-b-sqrt(disc))/(2. a) Write message
that equation has two distinct real roots Write
x1 and x2 ELSE IF disc lt 0 THEN real_part ?
-b/(2. a) Imag_part ? sqrt(abs(disc))/(2.
a) Write message that equation has two complex
roots Write the two complex roots ELSE x1 ?
-b/(2. a) Write message that equation has two
identical real roots Write x1 END IF
17- Turn it into Fortran statements
Ask user to enter coefficients Read coefficient
a, b, c Calculate discriminant disc ? b2 - 4 a
c IF disc gt 0 THEN x1 ? (-bsqrt(disc))/(2.
a) x2 ? (-b-sqrt(disc))/(2. a) Write message
that equation has two distinct real roots Write
x1 and x2 ELSE IF disc lt 0 THEN real_part ?
-b/(2. a) Imag_part ? sqrt(abs(disc))/(2.
a) Write message that equation has two complex
roots Write the two complex roots ELSE x1 ?
-b/(2. a) Write message that equation has two
identical real roots Write x1 END IF
18- Turn it into Fortran statements
PROGRAM roots IMPLICIT NONE REAL a, b, c,
disc, imag_part, real_part, x1,x2 WRITE (,)
'Enter the three coefficints a,b, and c ' READ
(,) a, b, c disc b2 - 4. a c IF (disc gt
0.) THEN x1 (-b SQRT(disc))/(2. a) x2 (-b
- SQRT(disc))/(2. a) WRITE (,) ' The equation
has two distict real roots.' WRITE (,) ' X1 ',
x1, ' X2 ', x2 ELSE IF (disc lt 0.)
THEN real_part (-b)/(2. a) imag_part
SQRT(ABS(disc))/(2. a) WRITE (,) ' The
equation has two complex roots.' WRITE (,) '
X1 ', real_part, ' i', imag_part, ' X2 ',
real_part, '- i', imag_part ELSE x1 (-b)/(2.
a) WRITE (,) ' The equation has two identical
real roots.' WRITE (,) ' X1 X2 ', x1 END
IF END PROGRAM
19GRADES EXAMPLE
- PROGRAM GRADES
- IMPLICIT NONE
- REAL GRADE
- WRITE (,) "ENTER YOUR GRADE (out of 100)"
- WRITE (,) ""
- READ (,) GRADE
- IF (GRADE gt 95.0) THEN
- WRITE (,) 'THE GRADE IS A.'
- ELSE IF (GRADE gt 86.0) THEN
- WRITE (,) 'THE GRADE IS B.'
- ELSE IF (GRADE gt 76.0) THEN
- WRITE (,) 'THE GRADE IS C.'
- ELSE IF (GRADE gt 66.0) THEN
- WRITE (,) 'THE GRADE IS D.'
- ELSE
- WRITE (,) 'THE GRADE IS F.'
20IF, ELSE IF and ELSE
- READING ASSIGNMENT
- EXAMPLE 3-3 (PAGE 97)
- You should read the above example which
illustrates the steps of designing a program
using IF statements - Problem statement
- Defining inputs and outputs
- Algorithm design
- Task ? subtasks
- pseudocode and flowchart
- Turn algorithm into Fortran statements
- Test
21Naming Nested IFs
- PROGRAM mixup
-
- IF (test1) THEN
-
-
- END IF
- IF (test2) THEN
-
-
- END IF
- IF (test3) THEN
-
-
- END IF
- END PROGRAM mixup
-
22Naming Nested IFs
- PROGRAM mixup
-
- outer IF (test1) THEN
-
-
- middle IF (test2) THEN
-
-
- inner IF (test3) THEN
-
-
- END IF inner
-
- END IF middle
-
- END IF outer
-
- END PROGRAM mixup
-
Nested IF ( one or more IF block inside another
one)
23Naming Nested IFs
- PROGRAM mixup
-
- outer IF (test1) THEN
-
-
- middle IF (test2) THEN
-
-
- inner IF (test3) THEN
-
-
- END IF inner
-
- END IF middle
-
- END IF outer
-
- END PROGRAM mixup
-
Naming IF ( up to 31 alphanumeric)
24Naming Nested IFs
What is the advantage of naming IF blocks?
- outer IF (test1) THEN
-
-
- middle IF (test2) THEN
-
-
- inner IF (test3) THEN
-
-
- END IF inner
-
- END IF middle
-
- END IF outer
-
outer IF (test1) THEN middle
IF (test2) THEN
inner IF (test3) THEN
END IF inner END IF
middle END IF outer
25Naming Nested IFs
What is the advantage of naming IF blocks?
- outer IF (test1) THEN
-
-
- middle IF (test2) THEN
-
-
- inner IF (test3) THEN
-
-
- END IF inner
-
- END IF middle
-
- END IF outer
-
outer IF (test1) THEN middle
IF (test2) THEN
inner IF (test3) THEN
END IF inner END IF
middle END IF outer
END IF missing
Compiler
26Naming Nested IFs
- PROGRAM GRADES
- IMPLICIT NONE
- REAL GRADE
- WRITE (,) "ENTER YOUR GRADE (out of 100)"
- WRITE (,) ""
- READ (,) GRADE
- IF (GRADE gt 95.0) THEN
- WRITE (,) 'THE GRADE IS A.'
- ELSE IF (GRADE gt 86.0) THEN
- WRITE (,) 'THE GRADE IS B.'
- ELSE IF (GRADE gt 76.0) THEN
- WRITE (,) 'THE GRADE IS C.'
- ELSE IF (GRADE gt 66.0) THEN
- WRITE (,) 'THE GRADE IS D.'
- ELSE
- WRITE (,) 'THE GRADE IS F.'
PROGRAM GRADES IMPLICIT NONE REAL
GRADE WRITE (,) "ENTER YOUR GRADE (out of
100)" WRITE (,) "" READ (,) GRADE IF (GRADE
gt 95.0) THEN WRITE (,) 'THE GRADE IS A. ELSE
IF (GRADE gt 86.0) THEN WRITE (,) 'THE GRADE
IS B. ELSE IF (GRADE gt 76.0) THEN
WRITE (,) 'THE GRADE IS C. ELSE IF
(GRADE gt 66.0) THEN WRITE (,)
'THE GRADE IS D. ELSE
WRITE (,) 'THE GRADE IS F. END
IF END IF END IF END IF END PROGRAM
27Special IF construct
- One line statement that is equivalent to if block
with one statement - IF (logical _expr) Statement
- IF (mark gt 95) grade A
28SELECT CASE
- Another branching method
- Used to select a block of code to execute based
on the value of a single integer, character, or
logical expression - General Form
- SELECT CASE (Case_expr)
- CASE (selector_1)
- Statement 1
- Statement 2
-
- CASE (selector_2)
- Statement 1
- Statement 2
-
- CASE DEFAULT
- Statement 1
- Statement 2
-
- END SELECT
29GRADES EXAMPLE
- PROGRAM GRADES
- IMPLICIT NONE
- INTEGER GRADE 98
- WRITE (,) "ENTER YOUR GRADE"
- WRITE (,) ""
- READ (,) GRADE
- SELECT CASE (NINT(GRADE))
- CASE (101)
- WRITE (,) 'GRADE CANNOT EXCEED 100'
- CASE (95100)
- WRITE (,) 'THE GRADE IS A.'
- CASE (8694)
- WRITE (,) 'THE GRADE IS B.'
- CASE (7685)
- WRITE (,) 'THE GRADE IS C.'
- CASE (6675)
- WRITE (,) 'THE GRADE IS D.'
- CASE DEFAULT
30SELECT CASE
- Forms of case selectors (RANGES)
- CASE ( value1 value2 ) value1 to value2
- CASE ( value1 ) value1 and above
- CASE ( value2 ) value2 and below
- CASE ( value ) a single value
- CASE (value1, value2, value3, value4) a list of
values
31SELECT CASE
- Forms of case selectors (RANGES)
- CASE ( 1 10 )
- CASE ( 10 )
- CASE ( 10 )
- CASE ( 7 )
- CASE ( 3, 4, 7)
- Reading assignment
- Example 3-5 (Page 107) Selecting day of the week
32Example Months Name
33Example Months Name
- program month
- implicit none
- CHARACTER(LEN9) month_name
- INTEGER month_number
- WRITE (,) "Enter the month of the year (1-12)"
- WRITE (,) ""
- READ (,) month_number
- SELECT CASE (month_number)
- CASE (1)
- WRITE (,) 'JANUARY'
- CASE (2)
- WRITE (,) 'FEBRAURY'
- CASE (3)
- WRITE (,) 'MARCH'
- CASE (4)
- WRITE (,) 'APRIL'
34Example Months Name
- CASE (5)
- WRITE (,) 'MAY'
- CASE (6)
- WRITE (,) 'JUNE'
- CASE (7)
- WRITE (,) 'JULY'
- CASE (8)
- WRITE (,) 'AUGUST'
- CASE (9)
- WRITE (,) 'SEPTEMBER'
- CASE (10)
- WRITE (,) 'OCTOBER'
- CASE (11)
- WRITE (,) 'NOVEMBER
- CASE (12)
- WRITE (,) 'DECEMBER'
- CASE DEFAULT
- WRITE (,) Error out of range'
- END SELECT