Introduction to Fortran

Simple Program Construction in Fortran » Making Decisions

 program pick_outfit implicit none  integer high_temp logical weekend  end 

Neither of the variables we have declared are assigned values yet. Those two variables can be set with the same style of code:

 ! temperature in degrees Fahrenheit high_temp = 64 ! logical when weekend is .TRUE., it is a weekend,  ! when weekend is .FALSE., it is a workday weekend = .TRUE. 

The single equals sign (=) is used to assign values to variables of any type. Based on the code above it is a weekend with a forecast high temperature of 64˚F.

Let’s use that data to start making some of the decisions in our program.

The pseudocode we are trying to codify is below:

 1. If today is a weekend        a. If the forecast temperature is at or above 70˚F, select a t-shirt,
else select a long-sleeved top. b. If the forecast temperature is above 60˚F, select athletic shorts,
else select pajama pants. else (today is a workday) a. If the forecast temperature is at or above 70˚F, select a short-sleeved top,
else select a long-sleeved dress top. b. If the forecast temperature is above 60˚F, select shorts,
else select pants.

To codify these, we need to understand the Fortran if-else statement. These pseudocode snippets look a lot like what we are going to end up using:

 if (logical expression) then      statement else      statement end if 

The code bits that are bold and italicized are to be replaced by your code. The other code is the definition of an if-else statement. The logical expression is a test of a logical statement. For us, this would be:

 if (weekend .EQ. .TRUE.) then  else  end if 

Notice the difference between assigning values to variables and testing a logical expression. In Fortran 77 and below, logical expressions can be evaluated with:

  • .EQ. is equals
  • .NE. is not equal to
  • .GT. is greater than
  • .GE. is greater than or equal to
  • .LT. is less than
  • .LE. is less than or equal to

In Fortran 90 and above, logical expressions can be evaluated with:

  • = is equals
  • != is not equal to
  • > is greater than
  • >= is greater than or equal to
  • < is less than
  • <= is less than or equal to

The program will enter into the else portion of the if-else statement when the if’s logical expression evaluates to .FALSE.

Question 1 of 1

Can you rewrite the if-else statement below to be equivalent but use a different logical expression?


There is only one other way to write the code using the weekend logical variable and make the if portion of the if-else statement evaluate to a weekend.

 if (weekend .NE. .FALSE.) then else end if