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
if / then / elsedo (loop)
📋 Array
🧩 Functions & Subroutines
Fortran›REFERENCE›Condition & Loop
🔁

Condition & Loop

Decision making and repetition.

CommandDescription
if / then / elseExecutes code based on a condition.
do (loop)Numeric loop.

if / then / else

Try it yourself →

Executes code based on a condition.

Syntax

if (condition) then ... else ... end if

Description

Condition inside parenthesis. Comparison == /= < > or .eq. .ne.; logic .and. .or. .not.

Example

program h
  integer :: x
  x = 7
  if (mod(x,2) == 0) then
    print *, "even"
  else
    print *, "odd"
  end if
end program h

Expected output

odd

When to use?

Flow branching.

do (loop)

Try it yourself →

Numeric loop.

Syntax

do i = start, end
  ...
end do

Description

do i = 1, 5 → from 1 to 5 (BOTH inclusive). The third value is the step: do i=0,10,2.

Example

program h
  integer :: i, t
  t = 0
  do i = 1, 10
    t = t + i
  end do
  print *, t
end program h

Expected output

55

When to use?

Summation, array processing, iteration.

Common mistakes

  • ⚠️ do i=1,5 → the final value 5 IS INCLUDED.
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