View
217
Download
2
Embed Size (px)
Citation preview
3
The “IF” statement
The “IF” statement is very useful!!!
It allows you to “steer” a Fortran program in one direction or another (or multiple directions).
p.48 shows the “flow control”
9/15/2011MET 50, FALL 2011, CHAPTER 3 PART 1
MET 50, FALL 2011, CHAPTER 3 PART 1
4
The “IF” statement
Easiest version:
IF (expression) (statement)
Meaning:
if the “expression” is true, the “statement” is executed.
if the “expression” is false, the “statement” is not executed.
9/15/2011
MET 50, FALL 2011, CHAPTER 3 PART 1
5
The “IF” statement
In this case:
“statement” must be a single statement.
Examples:IF (“X is larger than 5”) PRINT*,XIF (“TEMP negative”) TEMP = 9999.9IF (“X is positive”) Y = SQRT(X)
9/15/2011
MET 50, FALL 2011, CHAPTER 3 PART 1
6
The “IF” statement
InIF (expression) (statement)
What can “expression” look like?
In Fortran77:
IF (X . GE . 0.) Y = SQRT(X)
This is called a logical expression
9/15/2011
MET 50, FALL 2011, CHAPTER 3 PART 1
7
The “IF” statement
The logical expression…
X . GE . 0.
can only have two values: TRUE or FALSE.
9/15/2011
MET 50, FALL 2011, CHAPTER 3 PART 1
8
The “IF” statement
Thus with
IF (X . GE . 0.) Y = SQRT(X)
When X 0, we DO execute Y = SQRT(X)
When X < 0, we do NOT execute Y = SQRT(X)
9/15/2011
MET 50, FALL 2011, CHAPTER 3 PART 1
9
The “IF” statement
In Fortran90, this logical expression has become:
IF (X > 0) Y = SQRT(X)
9/15/2011
F90 F77 Meaning
< .LT. Less than
<= .LE. Less than or equal to
> .GT. Greater than
>= .GE. Greater than or equal to
== .EQ. Equal to
/= .NE. Not equal to
MET 50, FALL 2011, CHAPTER 3 PART 1
10
The “IF” statement
Next simplest version:
IF (expression) THENstatements (usually multiple!)ENDIF (or can be END
IF)
Meaning:
if the “expression” is true, the statements are executed.
if the “expression” is false, the statements are not executed.
9/15/2011
MET 50, FALL 2011, CHAPTER 3 PART 1
11
The “IF” statement
Example:
IMPLICIT NONEREAL :: A, B, C, QUANT, ANSREAD*, A, B, CQUANT = A**2 – 4.*B*CIF (QUANT >= 0.) THENANS = SQRT(QUANT)PRINT*,’RESULT IS’, ANSEND IF
9/15/2011
MET 50, FALL 2011, CHAPTER 3 PART 1
12
The “IF” statement
Good programming practice – indent!
IMPLICIT NONEREAL :: A, B, C, QUANT, ANSREAD*, A, B, CQUANT = A**2 – 4.*B*CIF (QUANT >= 0.) THEN ANS = SQRT(QUANT) PRINT*,’RESULT IS’, ANSEND IF
9/15/2011
MET 50, FALL 2011, CHAPTER 3 PART 1
13
The “IF” statement
The logical expressions can be combined using:
IF (QUANT >= 0. .AND. B > 4. ) THEN ANS = SQRT(QUANT) PRINT*,’RESULT IS’, ANSEND IF
9/15/2011
F90 Meaning
.AND. Both true
.OR. One true
.NOT. Nothing (?)
MET 50, FALL 2011, CHAPTER 3 PART 1
14
The “IF” statement
IF (QUANT >= 0. .OR. B > 4. ) THEN ANS = SQRT(QUANT) PRINT*,’RESULT IS’, ANSEND IF
9/15/2011
MET 50, FALL 2011, CHAPTER 3 PART 1
15
The “IF” statement
The IF – ELSE construct:
IF (expression1) THENstatements1
ELSEstatements2
ENDIF
9/15/2011
MET 50, FALL 2011, CHAPTER 3 PART 1
16
The “IF” statement
IF (QUANT >= 0.) THENANS = SQRT(QUANT)PRINT*,’ANSWER’,ANS
ELSEPRINT*,’REQUIRES SQRT(NEG
NO)’ENDIF
9/15/2011
MET 50, FALL 2011, CHAPTER 3 PART 1
17
The “IF” statement
The IF – ELSEIF construct:
IF (expression1) THENstatements1
ELSE IF (expression2) THENstatements2
ELSE IF (expression3) THENstatements3
ENDIF
9/15/2011
MET 50, FALL 2011, CHAPTER 3 PART 1
18
The “IF” statement
IF (TEMP >= 90.) THENPRINT*,’IT IS HOT!’
ELSE IF (TEMP >= 80. .AND. TEMP < 90.) THENPRINT*,’IT IS WARM’
ELSE IF (TEMP >= 70. .AND. TEMP < 80.) THENPRINT*,’IT IS NICE’
ELSE ! note that the last one can be just ELSEPRINT*,’IT IS NOT NICE’
ENDIF
9/15/2011
MET 50, FALL 2011, CHAPTER 3 PART 1
19
The “IF” statement
So the “IF” code structure is embedded in the program.
There can be many of these!
Can even have IF structures within IF structures!
example…
9/15/2011
MET 50, FALL 2011, CHAPTER 3 PART 1
20
The “IF” statement
IF (TEMP >= 90.) THENPRINT*,’IT IS HOT!’
IF (PRES > 1010.) THENPRINT*,’PRESSURE IS HIGH’
IF (HUM < 10.) THENPRINT*,’HUMIDITY UNDER 10%’ENDIF
ENDIFENDIF
Note how we indent code to make segments easy to see and find.
9/15/2011