An if (cond) then ... else ... end if block picks one branch. Comparisons return a logical value.

Program

Play the program to choose pass for a high score.

if_then_else.f90
Replay: real traced execution (multi-file project)
program if_then_else
    implicit none
    integer :: score
    character(len=8) :: grade
    score = 82
    if (score >= 80) then
        grade = "pass"
    else
        grade = "retry"
    end if
    print '(A)', trim(grade)
end program if_then_else
  1. score ← 82

    4character(len=8) :: grade5score = 826if (score >= 80) then
    values this step82score
  2. score >= 80 ← .true.

    5score = 826if (score >= 80) then7    grade = "pass"
    values this step.true.score >= 8082score
  3. grade ← pass

    6if (score >= 80) then7    grade = "pass"8else
    values this steppassgrade
  4. print '(A)', trim(grade)

    10    end if11    print '(A)', trim(grade)12end program if_then_else
    outputpass
    values this steppassgrade

Choose the Branch

  1. score starts at 82.
  2. score >= 80 evaluates to .true..
  3. The then branch sets grade to pass.
  4. trim(grade) removes padding before printing. | Score | Condition | Printed grade | | --- | --- | --- | | 82 | .true. | pass | | below 80 | .false. | retry |
if block `if ... then ... else ... end if` chooses a branch.
logical `score >= 80` evaluates to `.true.` or `.false.`.
trim `trim` drops trailing blanks before printing.

Exercise: if_then_else.f90

Use if/then/else to print pass for a high score and retry otherwise