Skip to main content

6.8) Understanding Loops (cont'd)


       integer :: i
       do i=1,100
          print *, i
       enddo

When the program runs, this segment of code above will first declare an integer on line 1, then it would enter the ‘do loop’ on line 2. Since the program has come from line 1 to line 2 (i.e. came to the loop from above) the loop will start with the value i=1.

Now it enters inside the loop (line 3) and it prints the value of i which is 1. Then it goes to line 4 which is the end of the do loop and it loops back up to line 2. Since the program has looped back up from below (line 4 to line 2) then i is increased by 1 to i=2. It then goes inside the loop and prints i (which equals 2). It then goes to line 4 and loops back up and i is increased by 1 to i=3. It goes into the loop and prints the value of I (which now equals 3) and then the loop is done, since i has reached the upper limit of 3 as stated on line 2.