typestar

do loops in Fortran

The counted do, plus cycle and exit steering from inside the body.

program do_loops
    implicit none
    integer :: i

    ! A counted do runs start, end, and an optional stride.
    do i = 2, 8, 2
        print *, "sled dog lap ", i
    end do

    ! cycle skips a pass; exit leaves the loop entirely.
    do i = 1, 10
        if (mod(i, 2) == 1) cycle
        if (i > 6) exit
        print *, "even and small: ", i
    end do
end program do_loops

How it works

  1. do i = 2, 8, 2 runs start, end, stride — the loop variable is real data.
  2. cycle skips to the next pass when the lap is odd.
  3. exit leaves the loop entirely once the numbers grow past 6.

Keywords and builtins used here

The run, in numbers

Lines
16
Characters to type
318
Tokens
61
Three-star pace
70 tpm

At the three-star pace of 70 tokens a minute, this run takes about 52 seconds.

Type this snippet

Step 3 of 3 in Control flow, step 9 of 27 in Language basics.

← Previous Next →