# Some explanation on do loop

**URL:** <https://fortran-lang.discourse.group/t/some-explanation-on-do-loop/7803>\
**Category:** Help\
**Created:** [April 9, 2024, 5:50pm UTC](https://fortran-lang.discourse.group/t/some-explanation-on-do-loop/7803 "2024-04-09T17:50:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rmoortgat](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/rmoortgat/32/3569_2.png) [@rmoortgat](https://fortran-lang.discourse.group/u/rmoortgat)\
**Post date:** [April 9, 2024, 5:50pm UTC](https://fortran-lang.discourse.group/t/some-explanation-on-do-loop/7803/1 "2024-04-09T17:50:03Z")

</div>

Good evening,

Could someone explain the following do loop. I suppose this is someting very elementary but I don’t see it.  
I see an integer i used only once and I see two labels in the do loop.  
I tought labels were obsolete.

```auto
program ch0505
implicit none
real :: p = 0.4e-4, papprox = 0.41e-4
real :: abs_error, rel_error
integer :: i
do i = 1, 3
abs_error = abs(p-papprox)
rel_error = abs(p-papprox)/abs(p)
print 100, p, papprox
100 format (’p = ’, e11.4, /, &
’papprox = ’, e11.4)
print 110, abs_error, rel_error
110 format (’abs error:’, 12x, e11.4, /, &
’rel error:’, 12x, e11.4, /)
p = p*1.0e5
papprox = papprox*1.0e5
end do
end program ch0505

```

Thx  
Roger

---

<div class="post-metadata">

**Author:** ![PierU](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pieru/32/1848_2.png) [@PierU](https://fortran-lang.discourse.group/u/PierU)\
**Post date:** [April 9, 2024, 6:05pm UTC](https://fortran-lang.discourse.group/t/some-explanation-on-do-loop/7803/2 "2024-04-09T18:05:39Z")

</div>

To me it just illustrates the difference between an absolute error and a relative error.

And no, labels are not obsolete in such a context (they are used to refer to a `format` definition).

Note that as pasted, this code contains ticks (`’` , which are invalid Fortran characters) instead of single quotes (`'`). Once corrected the output is:

```auto
p = 0.4000E-04
papprox = 0.4100E-04
abs error: 0.1000E-05
rel error: 0.2500E-01

p = 0.4000E+01
papprox = 0.4100E+01
abs error: 0.1000E+00
rel error: 0.2500E-01

p = 0.4000E+06
papprox = 0.4100E+06
abs error: 0.1000E+05
rel error: 0.2500E-01

```

---

<div class="post-metadata">

**Author:** ![rmoortgat](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/rmoortgat/32/3569_2.png) [@rmoortgat](https://fortran-lang.discourse.group/u/rmoortgat)\
**Post date:** [April 9, 2024, 6:33pm UTC](https://fortran-lang.discourse.group/t/some-explanation-on-do-loop/7803/3 "2024-04-09T18:33:44Z")

</div>

Thank you, I’m aware of what the program is doing and it runs fine here.  
I guess I didn"t formulate my problem well.  
I just don’t understand what happens when you enter the do loop.  
I suppose the loop runs for i = 1, 2, 3.  
But where does i get incremented?  
I copied from a pdf so I suppose this is the reason of the invalid characters.  
Guess I’ll try to run line be line to see what happens within the do loop.

Roger

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [April 9, 2024, 6:42pm UTC](https://fortran-lang.discourse.group/t/some-explanation-on-do-loop/7803/4 "2024-04-09T18:42:51Z")

</div>

> [@rmoortgat](#):
>
> But where does i get incremented?

`i` is incremented automatically (+1 by default) each time you arrive at the `end do`, it is then compared with the right bound (3) and the loop continues if `i<=3`.

In C, it would be:

```C
for (int i=1 ; i<=3 ; i++) {

}

```

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [April 9, 2024, 6:51pm UTC](https://fortran-lang.discourse.group/t/some-explanation-on-do-loop/7803/5 "2024-04-09T18:51:23Z")

</div>

> [@rmoortgat](#):
>
> I tought labels were obsolete.

Labels are not obsolete, but personnally I would rather write:

```fortran
print '("p = ", e11.4, /, "papprox = ", e11.4)', p, papprox

```

If you use the format several times, you can even store it in a string:

```fortran
character(*), parameter :: formatting = '("p = ", e11.4, /, "papprox = ", e11.4)'
...
print formatting, p, papprox

```

---

<div class="post-metadata">

**Author:** ![wspector](https://avatars.discourse-cdn.com/v4/letter/w/47e85d/32.png) [@wspector](https://fortran-lang.discourse.group/u/wspector)\
**Post date:** [April 9, 2024, 8:31pm UTC](https://fortran-lang.discourse.group/t/some-explanation-on-do-loop/7803/6 "2024-04-09T20:31:32Z")

</div>

The number of trips through the loop is computed before the loop begins. The index variable, in this case ‘i’, is assigned a new value on each iteration.

Note that zero trips through a loop is a possibility. For example:

do, i=1, 0  
… ! Nothing in here will be performed.  
end do

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [April 10, 2024, 3:47am UTC](https://fortran-lang.discourse.group/t/some-explanation-on-do-loop/7803/7 "2024-04-10T03:47:43Z")

</div>

> [@wspector](#):
>
> The number of trips through the loop is computed before the loop begins. The index variable, in this case ‘i’, is assigned a new value on each iteration.

Normally that last sentence would be correct, but in this case the variable `i` is never referenced, so it is likely that it is never stored and/or incremented. The only way to tell if the compiler has done this would be to examine the machine code or to use a debugger and to monitor the storage location (if it exists) for` i`. If the code is modified to reference `i` somehow, then of course the compiler would not eliminate it.

So the real magic for the do loop is the trip count. That is usually computed as a nonnegative value at the beginning of the loop and decremented by 1 each pass through the loop until it reaches 0. The test is typically at the beginning of the loop and the decrement is at the end of the loop, but a compiler is free to implement loops any way it wants with any available hardware instructions as long as the correct number of passes occurs.

---

<div class="post-metadata">

**Author:** ![rmoortgat](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/rmoortgat/32/3569_2.png) [@rmoortgat](https://fortran-lang.discourse.group/u/rmoortgat)\
**Post date:** [April 10, 2024, 7:35am UTC](https://fortran-lang.discourse.group/t/some-explanation-on-do-loop/7803/8 "2024-04-10T07:35:01Z")

</div>

Thank you all for your kind replies.

Roger
