HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to FortranVariablesOperatorsConditions (if)Loops (do)ArraysFunctions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
🔁 Condition & Loop
📋 Array
🧩 Functions & Subroutines
Fortran›Conditions (if)

Conditions (if)

if-then-else and comparison/logical operators (==, .and., .or.).

Simple enough for a 5-year-old to understand

if / then / else

Example code
1
program h
2
integer :: score
3
score = 75
4
if (score >= 50) then
5
print *, "Passed"
6
else
7
print *, "Failed"
8
end if
End of block.
ConditionEnd of block.
9
end program h
Condition
💡 Hint: Comparison: == /= < > (or .eq. .ne.). Logic: .and. .or. .not.

About this topic

In Fortran, conditions are written with the expression in parentheses, the then keyword, and an end line. The block is always explicitly closed; indentation has no semantic meaning but is practically mandatory for readability.

For single-statement conditions, a single-line syntax without then is also available. It is concise and readable, but if someone later wants to add a second statement and forgets to convert it to block format, a silent bug occurs.

For multi-way decisions, a select construct is used. It works with integer, character, and logical values, supports range and list syntax, and there is no fall-through between branches.

What you will learn

  • Condition block structure
  • Single-line condition
  • the else if chain
  • the select construct
  • the risk of real number comparison

Who is this for?

  • Engineers learning Fortran
  • Developers writing decision logic
  • Those dealing with floating-point comparison

When to use?

For multi-way decisions based on constant values, the select construct is both more readable and faster than long chains of conditions.

Real number comparisons should always be performed using a tolerance value; looking for direct equality is a source of errors in scientific code.

Common mistakes

Wrong

program test
  implicit none
  real :: x
  x = 0.1 + 0.2
  if (x == 0.3) then
    print *, 'equal'
  else
    print *, 'not equal'
  end if
end program test

Floating-point numbers cannot represent decimal fractions exactly; the sum may not equal the expected value. In scientific computing, real numbers are never compared with direct equality; instead, you check whether the absolute difference is less than a small tolerance.

Correct

program test
  implicit none
  real :: x
  x = 0.1 + 0.2
  if (abs(x - 0.3) < 1.0e-6) then
    print *, 'equal'
  else
    print *, 'not equal'
  end if
end program test

Wrong

program test
  implicit none
  integer :: a
  a = 5
  if (a > 3) print *, 'greater'
            print *, 'this is also thought to be conditional'
end program test

In the single-line condition format, only the statement on the same line is conditional; the second line runs in all cases even if it is indented. If you want to add a second statement, you must switch to the block format.

Correct

program test
  implicit none
  integer :: a
  a = 5
  if (a > 3) then
    print *, 'greater'
    print *, 'both are conditional'
  end if
end program test

Wrong

program test
  implicit none
  integer :: a
  a = 5
  if (a > 3) then
    print *, 'greater'
end program test

Because the condition block is not closed, the compiler encounters an unexpected end of program. Fortran requires you to always explicitly close blocks; it ignores indentation. The error message is sometimes confusing because it points to a line far away.

Correct

program test
  implicit none
  integer :: a
  a = 5
  if (a > 3) then
    print *, 'greater'
  end if
end program test

Frequently asked questions

Can I name blocks?

Yes, you can give a name to a conditional or loop block and repeat that name in the end line. In deeply nested blocks, this makes it clear which one is closing, and the compiler catches mismatched ends.

What types does the select construct work with?

Integer, character, and logical values. It does not work with real numbers because equality comparison is unreliable for this type. Ranges and comma-separated list syntax are supported.

How do I choose the tolerance value?

Based on the magnitude of the values you are comparing. It is safer to use a relative difference instead of a fixed threshold: you divide the difference by the magnitude of the values and compare it against the threshold.

Summary

Condition blocks are explicitly closed.

A single-line condition binds only one expression.

Real numbers are compared with a tolerance.

The select construct does not work with real numbers.

Blocks can be named to verify matching.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code