Control Flow
If/Then/Else
Block Choice
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
score ← 82
4character(len=8) :: grade5score = 826if (score >= 80) thenvalues this step82scorescore >= 80 ← .true.
5score = 826if (score >= 80) then7 grade = "pass"values this step.true.score >= 8082scoregrade ← pass
6if (score >= 80) then7 grade = "pass"8elsevalues this steppassgradeprint '(A)', trim(grade)
10 end if11 print '(A)', trim(grade)12end program if_then_elseoutputpassvalues this steppassgrade
Choose the Branch
scorestarts at82.score >= 80evaluates to.true..- The
thenbranch setsgradetopass. trim(grade)removes padding before printing. | Score | Condition | Printed grade | | --- | --- | --- | |82|.true.|pass| | below80|.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