Presentation is loading. Please wait.

Presentation is loading. Please wait.

FORTRAN SELECTIVE EXECUTION MET 50. THE VERY WONDERFUL “IF” STATEMENT MET 50.

Similar presentations


Presentation on theme: "FORTRAN SELECTIVE EXECUTION MET 50. THE VERY WONDERFUL “IF” STATEMENT MET 50."— Presentation transcript:

1 FORTRAN SELECTIVE EXECUTION MET 50

2 THE VERY WONDERFUL “IF” STATEMENT MET 50

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/2011 3 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 4 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*,X IF (“TEMP negative”) TEMP = 9999.9 IF (“X is positive”) Y = SQRT(X) 9/15/2011 5 MET 50, FALL 2011, CHAPTER 3 PART 1

6 The “IF” statement In IF (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 6 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 7 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 8 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 9 MET 50, FALL 2011, CHAPTER 3 PART 1 F90F77Meaning <.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

10 The “IF” statement Next simplest version: IF (expression) THEN statements (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 10 MET 50, FALL 2011, CHAPTER 3 PART 1

11 The “IF” statement Example: IMPLICIT NONE REAL :: A, B, C, QUANT, ANS READ*, A, B, C QUANT = A**2 – 4.*B*C IF (QUANT >= 0.) THEN ANS = SQRT(QUANT) PRINT*,’RESULT IS’, ANS END IF 9/15/2011 11 MET 50, FALL 2011, CHAPTER 3 PART 1

12 The “IF” statement Good programming practice – indent! IMPLICIT NONE REAL :: A, B, C, QUANT, ANS READ*, A, B, C QUANT = A**2 – 4.*B*C IF (QUANT >= 0.) THEN ANS = SQRT(QUANT) PRINT*,’RESULT IS’, ANS END IF 9/15/2011 12 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’, ANS END IF 9/15/2011 13 MET 50, FALL 2011, CHAPTER 3 PART 1 F90Meaning.AND.Both true.OR.One true.NOT.Nothing (?)

14 The “IF” statement IF (QUANT >= 0..OR. B > 4. ) THEN ANS = SQRT(QUANT) PRINT*,’RESULT IS’, ANS END IF 9/15/2011 14 MET 50, FALL 2011, CHAPTER 3 PART 1

15 The “IF” statement The IF – ELSE construct: IF (expression1) THEN statements1 ELSE statements2 ENDIF 9/15/2011 15 MET 50, FALL 2011, CHAPTER 3 PART 1

16 The “IF” statement IF (QUANT >= 0.) THEN ANS = SQRT(QUANT) PRINT*,’ANSWER’,ANS ELSE PRINT*,’REQUIRES SQRT(NEG NO)’ ENDIF 9/15/2011 16 MET 50, FALL 2011, CHAPTER 3 PART 1

17 The “IF” statement The IF – ELSEIF construct: IF (expression1) THEN statements1 ELSE IF (expression2) THEN statements2 ELSE IF (expression3) THEN statements3 ENDIF 9/15/2011 17 MET 50, FALL 2011, CHAPTER 3 PART 1

18 The “IF” statement IF (TEMP >= 90.) THEN PRINT*,’IT IS HOT!’ ELSE IF (TEMP >= 80..AND. TEMP < 90.) THEN PRINT*,’IT IS WARM’ ELSE IF (TEMP >= 70..AND. TEMP < 80.) THEN PRINT*,’IT IS NICE’ ELSE! note that the last one can be just ELSE PRINT*,’IT IS NOT NICE’ ENDIF 9/15/2011 18 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 19 MET 50, FALL 2011, CHAPTER 3 PART 1

20 The “IF” statement IF (TEMP >= 90.) THEN PRINT*,’IT IS HOT!’ IF (PRES > 1010.) THEN PRINT*,’PRESSURE IS HIGH’ IF (HUM < 10.) THEN PRINT*,’HUMIDITY UNDER 10%’ ENDIF Note how we indent code to make segments easy to see and find. 9/15/2011 20 MET 50, FALL 2011, CHAPTER 3 PART 1

21 The “IF” statement Two things to ignore in Cht. 3: 1. The CASE construct 2. “named constructs” Next Tuesday: practice (IF you show up) (hahaha) 9/15/2011 21 MET 50, FALL 2011, CHAPTER 3 PART 1


Download ppt "FORTRAN SELECTIVE EXECUTION MET 50. THE VERY WONDERFUL “IF” STATEMENT MET 50."

Similar presentations


Ads by Google