1 / 21

MET 50

MET 50. Fortran SELECTIVE EXECUTION. MET 50. THE VERY WONDERFUL “IF” STATEMENT. 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”. The “IF” statement.

gerard
Download Presentation

MET 50

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MET 50 Fortran SELECTIVE EXECUTION

  2. MET 50 THE VERY WONDERFUL “IF” STATEMENT

  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” 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. 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) 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 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. 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) 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) MET 50, FALL 2011, CHAPTER 3 PART 1

  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 statementsare executed. if the “expression” is false, the statements are not executed. 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 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 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 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’, ANS END IF MET 50, FALL 2011, CHAPTER 3 PART 1

  15. The “IF” statement The IF – ELSE construct: IF (expression1) THEN statements1 ELSE statements2 ENDIF 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 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 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 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… 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 ENDIF ENDIF Note how we indent code to make segments easy to see and find. MET 50, FALL 2011, CHAPTER 3 PART 1

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

More Related