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›Loops (do)

Loops (do)

Numeric loop with do (end value INCLUDED) and accumulating the sum.

Simple enough for a 5-year-old to understand

do loop

do i = 1, 5 → from 1 to 5 (BOTH included). Different from other languages!

Example code
1
program h
2
integer :: i, t
3
t = 0
Accumulate the sum.
VariableAccumulate the sum.
4
do i = 1, 10
From 1 to 10 (10 included).
LoopFrom 1 to 10 (10 included).
5
t = t + i
6
end do
7
print *, t
55
Output / Input55
8
end program h
VariableLoopOutput / Input
💡 Hint: do i = 1, 5 loops five times — the end value is included.

About this topic

Fortran's basic loop takes a start, end, and optional step value. Bounds are inclusive and the step can be negative; a separate keyword is not needed to count backward.

There is also a conditional loop, which runs as long as the condition is true. Writing an infinite loop and breaking out of it conditionally is also a common pattern; exit and skip statements work in every loop form.

The way loops are written in numerical code directly affects performance. Fortran stores arrays in column-major order: in nested loops, having the innermost index be the first dimension significantly improves cache utilization.

What you will learn

  • The counted loop and the step value
  • The conditional loop
  • Exit and skip statements
  • The scope of the loop variable
  • The importance of nested loop order

Who is this for?

  • Engineers writing numerical code
  • Those seeking performance
  • Those falling into an infinite loop

When to use?

If the number of repetitions is known, a counted loop is used; the compiler can perform vectorization since it knows the number of iterations beforehand.

Matching the order of nested loops in two-dimensional arrays to the column-major layout can provide manifold speed gains without any algorithmic changes.

Common mistakes

Wrong

program test
  implicit none
  integer :: i
  i = 1
  do while (i <= 5)
    print *, i
  end do
end program test

Since the counter is not incremented, the condition remains true forever and the program never ends. In conditional loops, you have to ensure the progression yourself; if the number of repetitions is known, a counter-controlled loop eliminates this risk.

Correct

program test
  implicit none
  integer :: i
  i = 1
  do while (i <= 5)
    print *, i
    i = i + 1
  end do
end program test

Wrong

program test
  implicit none
  integer :: i
  do i = 1, 10
    if (i == 5) i = 8
    print *, i
  end do
end program test

The variable of a counted loop cannot be modified inside the body; the standard forbids this and compilers issue an error or warning. If you need to modify it, you must use a conditional loop or work with a separate variable.

Correct

program test
  implicit none
  integer :: i, deger
  do i = 1, 10
    deger = i
    if (i == 5) deger = 8
    print *, deger
  end do
end program test

Wrong

program test
  implicit none
  integer :: i, j
  real :: a(1000, 1000)
  do i = 1, 1000
    do j = 1, 1000
      a(i, j) = 0.0
    end do
  end do
end program test

Fortran stores arrays in column-major order; the first index is adjacent in memory. Here, since the innermost loop traverses the second index, it jumps to a distant location in memory at each step, and the cache is constantly invalidated. Reversing the loop order provides a noticeable speed gain.

Correct

program test
  implicit none
  integer :: i, j
  real :: a(1000, 1000)
  do j = 1, 1000
    do i = 1, 1000
      a(i, j) = 0.0
    end do
  end do
end program test

Frequently asked questions

How do I count backwards?

You provide a negative step as the third value in the loop header; the start value is greater than the end value. If the step is given as zero, the behavior is undefined and an infinite loop may occur.

How do I exit a loop early?

The exit statement ends the loop, and the skip statement skips that iteration. In nested loops, you can name a loop and use that name to specify which loop to exit from; this eliminates the need for flag variables.

Can I process the entire array without writing a loop?

Yes, Fortran allows direct operation on the entire array: adding two arrays or assigning a value to all of them is done in a single line. This format is both concise and more amenable to compiler vectorization.

Summary

Loop bounds are inclusive, a step can be provided.

The loop variable cannot be modified within the body.

In a conditional loop, you are responsible for advancing the progression.

Arrays are stored in column-major order.

Nested loop order determines performance.

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