210 likes | 234 Views
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.
E N D
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” MET 50, FALL 2011, CHAPTER 3 PART 1
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
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
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
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
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
The “IF” statement In Fortran90, this logical expression has become: IF (X > 0) Y = SQRT(X) MET 50, FALL 2011, CHAPTER 3 PART 1
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
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
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
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
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
The “IF” statement The IF – ELSE construct: IF (expression1) THEN statements1 ELSE statements2 ENDIF MET 50, FALL 2011, CHAPTER 3 PART 1
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
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
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
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
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
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