Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in...

34
Chapter 3 IF & SELECT

Transcript of Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in...

Page 1: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

Chapter 3

IF & SELECT

Page 2: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

Control 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

Page 3: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

IF.. 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

Page 4: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

IF.. statementLogical

Expression

Statement 1Statement 2

.FALSE.

.TRUE.IF (logical_expr) THENStatement 1Statement 2…END IF

Page 5: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

IF.. statement Example: Solving quadratic equation

PROGRAM QUADRATIC IMPLICIT NONE …… IF (b**2 – 4*a*c < 0) THEN WRITE (*,*) ‘There are two complex roots to this

equation.’ END IF …… END PROGRAM

2

2

0

4

2

ax bx c

b b acx

a

Page 6: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

IF.. statement Example: Solving quadratic equation

PROGRAM QUADRATIC IMPLICIT NONE …… IF (b**2 – 4*a*c < 0) THEN WRITE (*,*) ‘There are two complex roots to this

equation.’ END IF …… END PROGRAM

b**2 – 4*a*c < 0b**2 – 4*a*c < 0b**2 – 4*a*c < 0b**2 – 4*a*c < 0b**2 – 4*a*c < 0

WRITE ‘ There are two complex roots to

this equation.’

.FALSE.

.TRUE.

Page 7: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

IF, 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) THENStatement 1Statement 2…

ELSE IF (logical_expr2) THENStatement 1Statement 2…

ELSEStatement 1Statement 2…

END IF

Page 8: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

IF, ELSE IF and ELSE

b**2 – 4*a*c < 0b**2 – 4*a*c < 0b**2 – 4*a*c < 0b**2 – 4*a*c < 0b**2 – 4*a*c < 0

WRITE ‘ The equation has

complex roots.’

b**2 – 4*a*c > 0

WRITE ‘ The equation has two distinct real roots.’

WRITE ‘ The equation has two

identical real roots.’

.TRUE. .TRUE.

.FALSE. .FALSE.

END IF

Example:

Page 9: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

IF, ELSE IF and ELSE Example:

IF (b**2-4*a*c < 0.0) THENWRITE(*,*) ‘This equation has complex roots. ’

ELSE IF (b**2-4*a*c > 0.0) THENWRITE(*,*) ‘This equation has two distinct real roots. ’

ELSE WRITE(*,*) ‘This equation has two identical real roots.

’ END IF

Page 10: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

Example 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)1. Problem statement

2. Defining inputs and outputs

3. Algorithm design

1. Task subtasks

2. pseudo code or/and flowchart

4. Turn algorithm into Fortran statements

5. Test

Page 11: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

Example 3-2 (The Quadratic Equation) Problem

Design and write a program to solve for the roots of a quadratic equation regardless of type.

1. Problem statement (make it more clear) Design and write a program that calculates the two roots of the quadratic equation

a.x2+b.x+c=0. 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.

2

2

1,2

0

4

2

ax bx c

b b acx

a

Page 12: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

Example 3-2 (The Quadratic Equation)2. Defining inputs and outputs

• InputsEquation coefficients (real)

a, b, c

• OutputsRoots (real) and statement that describe their type

x1, x2 (complex, real distinct or identical)

2

2

1,2

0

4

2

ax bx c

b b acx

a

Page 13: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

Example 3-2 (The Quadratic Equation)3. Design Algorithm

Main tasksRead the input data (a,b,c)

Calculate the roots

Write the roots (x1, x2)

2

2

1,2

0

4

2

ax bx c

b b acx

a

Page 14: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

Example 3-2 (The Quadratic Equation)3. Design Algorithm

Main tasksRead the input data (a,b,c)

Calculate the roots

Write the roots (x1, x2)

2

2

1,2

0

4

2

ax bx c

b b acx

a

Ask user to enter coefficients

Read coefficient a, b, c

Calculate discriminant: disc ← b2 - 4 * a * c

IF disc > 0 THEN

Calculate two distinct real roots

Write the distinct real roots

ELSE IF disc < 0 THEN

Calculate complex roots

Write the two complex roots

ELSE

Calculate one real root

Write the repeated root

END IF

Page 15: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

Ask user to enter coefficients

Read coefficient a, b, c

Calculate discriminant: disc ← b2 - 4 * a * c

IF disc > 0 THEN

Calculate two distinct real roots

Write the distinct real roots

ELSE IF disc < 0 THEN

Calculate complex roots

Write the two complex roots

ELSE

Calculate one real root

Write the repeated root

END IF

Ask user to enter coefficients

Read coefficient a, b, c

Calculate discriminant: disc ← b2 - 4 * a * c

IF disc > 0 THEN

x1 ← (-b+sqrt(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 < 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

Page 16: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

Start

End

disc < 0

disc > 0

Write ‘Enter Coefficient’

WRITE x1, x2

WRITE ‘Eq. has two identical

real roots’

WRITEreal + i imgreal – i img

Calculate disc

Calculate real_part, img_part

Calculate x1, x2

WRITE x1

WRITE ‘Eq. has two

distinct real roots’

WRITE ‘Eq. has two complex

roots’

READ a,b,c

Calculate x1

.TRUE.

.FALSE.

.TRUE.

.FALSE.

Ask user to enter coefficients

Read coefficient a, b, c

Calculate discriminant: disc ← b2 - 4 * a * c

IF disc > 0 THEN

x1 ← (-b+sqrt(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 < 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

Page 17: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

Ask user to enter coefficients

Read coefficient a, b, c

Calculate discriminant: disc ← b2 - 4 * a * c

IF disc > 0 THEN

x1 ← (-b+sqrt(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 < 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

4. Turn it into Fortran statements

Page 18: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

PROGRAM rootsIMPLICIT NONEREAL :: a, b, c, disc, imag_part, real_part, x1,x2

WRITE (*,*) 'Enter the three coefficints a,b, and c' : READ (*,*) a, b, cdisc = b**2 - 4. *a * cIF (disc > 0.) THENx1 = (-b + SQRT(disc))/(2. * a)x2 = (-b - SQRT(disc))/(2. * a)WRITE (*,*) ' The equation has two distict real roots'.

WRITE (*,*) ' X1= ', x1, ' X2= ', x2ELSE IF (disc < 0.) THENreal_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_partELSEx1 = (-b)/(2. * a)WRITE (*,*) ' The equation has two identical real roots'.

WRITE (*,*) ' X1= X2 = ', x1END IFEND PROGRAM

4. Turn it into Fortran statements

Page 19: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

GRADES EXAMPLEPROGRAM GRADESIMPLICIT NONE

REAL :: GRADE

WRITE (*,*) "ENTER YOUR GRADE (out of 100)"WRITE (*,*) ""READ (*,*) GRADE

IF (GRADE > 95.0) THEN WRITE (*,*) 'THE GRADE IS A.'ELSE IF (GRADE > 86.0) THEN WRITE (*,*) 'THE GRADE IS B.'ELSE IF (GRADE > 76.0) THEN WRITE (*,*) 'THE GRADE IS C.'ELSE IF (GRADE > 66.0) THEN WRITE (*,*) 'THE GRADE IS D.'ELSE WRITE (*,*) 'THE GRADE IS F.'END IF

END PROGRAM

Page 20: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

IF, 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

Page 21: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

Naming & Nested IFsPROGRAM mixup…IF (test1) THEN … …END IF

IF (test2) THEN … …END IF

IF (test3) THEN … …END IFEND PROGRAM mixup

Page 22: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

Naming & Nested IFsPROGRAM 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(

Page 23: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

Naming & Nested IFsPROGRAM 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(

Page 24: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

Naming & Nested IFs

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

What is the advantage of naming IF blocks?

Page 25: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

Naming & Nested IFs

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

What is the advantage of naming IF blocks?

END IF missing END IF missingCompiler

Page 26: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

Naming & Nested IFsPROGRAM GRADESIMPLICIT NONE

REAL :: GRADE

WRITE (*,*) "ENTER YOUR GRADE (out of 100)"WRITE (*,*) ""READ (*,*) GRADE

IF (GRADE > 95.0) THEN WRITE (*,*) 'THE GRADE IS A.'ELSE IF (GRADE > 86.0) THEN WRITE (*,*) 'THE GRADE IS B.'ELSE IF (GRADE > 76.0) THEN WRITE (*,*) 'THE GRADE IS C.'ELSE IF (GRADE > 66.0) THEN WRITE (*,*) 'THE GRADE IS D.'ELSE WRITE (*,*) 'THE GRADE IS F.'END IF

END PROGRAM

PROGRAM GRADESIMPLICIT NONE

REAL :: GRADE

WRITE (*,*) "ENTER YOUR GRADE (out of 100)"WRITE (*,*) ""READ (*,*) GRADEIF (GRADE > 95.0) THEN

WRITE (*,*) 'THE GRADE IS A.’ELSE

IF (GRADE > 86.0) THENWRITE (*,*) 'THE GRADE IS B.’

ELSE IF (GRADE > 76.0) THEN WRITE (*,*) 'THE GRADE IS C.’ELSE IF (GRADE > 66.0) THEN WRITE (*,*) 'THE GRADE IS

D.’ ELSE WRITE (*,*) 'THE GRADE IS F.’ END IFEND IF

END IFEND IFEND PROGRAM

Page 27: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

Special IF construct• One line statement that is equivalent to if block with one statement

IF (logical _expr) Statement

IF (mark > 95) grade= ‘A’

Page 28: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

SELECT 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 1Statement 2…

CASE (selector_2)Statement 1Statement 2…

CASE DEFAULTStatement 1Statement 2…

END SELECT

Page 29: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

GRADES EXAMPLEPROGRAM GRADESIMPLICIT NONEINTEGER :: GRADE = 98WRITE (*,*) "ENTER YOUR GRADE"WRITE (*,*) ""READ (*,*) GRADE

SELECT CASE (NINT(GRADE)) CASE (101:) WRITE (*,*) 'GRADE CANNOT EXCEED 100%' CASE (95:100) WRITE (*,*) 'THE GRADE IS A.' CASE (86:94) WRITE (*,*) 'THE GRADE IS B.' CASE (76:85) WRITE (*,*) 'THE GRADE IS C.' CASE (66:75) WRITE (*,*) 'THE GRADE IS D.' CASE DEFAULT WRITE (*,*) 'THE GRADE IS F.'END SELECT

END PROGRAM

Page 30: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

SELECT 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

Page 31: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

SELECT 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

Page 32: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

Example: Month’s Name

Stop

Start

WRITE ‘ Enter month number’

READ month_number

CASE(1) CASE(2) CASE(3) CASE(10) CASE(11)Not in range Not in range ………. Not in range

WRITE ‘ JANUARY’

WRITE ‘ FEBRAURY’

WRITE ‘ MARCH’

WRITE ‘ OCTOBER’

WRITE ‘ NOVEMBER’

WRITE ‘ DECEMBER’

In range In range In range In range In range

Not in range

……….

Page 33: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

Example: Month’s Nameprogram monthimplicit none

CHARACTER(LEN=9) :: month_nameINTEGER :: 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'

Page 34: Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.

Example: Month’s 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 SELECTend program