1program sample
2 implicit none
3
4 integer, parameter :: lower = 2
5 integer, parameter :: upper = 9
6 integer, parameter :: stride = 3
7 integer :: i, j, sum
8
9 ! 一番シンプルな使い方
10 write(*, *) '--- Do loop #1 ---'
11 sum = 0
12 do i = 1, 10
13 sum = sum + i
14 write(*, *) i, sum
15 enddo
16
17 ! 2個飛ばしのループ
18 write(*, *) '--- Do loop #2 ---'
19 sum = 0
20 do i = 1, 10, 2
21 sum = sum + i
22 write(*, *) i, sum
23 enddo
24
25 ! 変数で範囲やストライドを指定可能
26 write(*, *) '--- Do loop #3 ---'
27 sum = 0
28 do i = lower, upper, stride
29 sum = sum + i
30 write(*, *) i, sum
31 enddo
32
33 ! 多重ループも可能
34 write(*, *) '--- Do loop #4 ---'
35 do i = 1, 3
36 do j = 1, 3
37 write(*, *) '(', i, ',', j, ') => ', 3 * (i - 1) + j
38 enddo
39 enddo
40
41 stop
42endprogram sample