004. exiting a do loop#

topic: Loops

You exit a loop as shown in the code below, which accumulates the sums of squared integers until the sum exceeds 100.

do-exit.f90 | | Godbolt Compiler Explorer logo | Fortran logo#
implicit none
integer :: i,ssq

ssq = 0
i = 0

do
   i = i+1
   ssq = ssq + i**2
   if (ssq > 100) exit
end do  
print *, "sum of squares from 1 to ", i, " is", ssq

end
Output1#
 sum of squares from 1 to            7  is         140


1

Compiled using GNU Fortran (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 with no flags