Fortran Do Loop | Tutorial 11

Table Of Contents

Introduction

We use computational method to perform many calculations. That means we use programming language (or certain code) to solve almost all type of complex problems. Why this?

One of the reasons behind this is: code can iterate over and over again according to our needs. I mean we can perform the same process so many times just by writing a single line of code. Cool, right?

This process of repeating the same task is done by loop and it’s the best feature of any programming language out there. Different languages have different ways of writing loop. In FORTRAN, we write a loop code using a DO STATEMENT

There are many things that one can achieve using DO loop. For a simple example, one can print out 100 same statements just writing that statement only one time. You will get so many examples as you go down. So let’s start learning Fortran DO LOOP.

If you haven’t gone through previous lessons then I suggest you to go through that using this LINK.

FORTRAN DO STATEMENT syntax

Ok, let’s see the syntax of DO STATEMENT

do integer_variable = start, stop, [step]
    statements.....
    ...............
end do

let’s see one simple example to see DO STATEMENT in action. Just copy or write the code below and run to see the output.

program example 
 implicit none
   integer :: i 
     do i = 1, 10
       print*, i
     end do
end program

Its output is:

1
2
3
4
5
6
7
8
9
10

So let’s see what’s happening in the code.

If you see highlighted code, you can see one print statement but we are getting 10 lines in the output (i.e from 1 to 10). This is because that print statement is wrapped inside DO and END DO statement which is telling to run that same code 10 times. How? Let’s learn step by step.

While writing DO STATEMENT, first we need one variable of the integer data type then we need a start and stop point (optional step) number for that integer. (see the syntax above)

When we write do i = 1, 10 that means i integer will hold value from 1 to 10 at each iteration( repition). So print*, i statement is executed 10 times but every time it executes, i value changes from 1 to 10 printing out 1 to 10 as output.

Now let’s write another code with new integer variable and with step part also.

program another 
  implicit none
    integer :: num
       do num = 5, 20, 3
          print*, num, 2*num
       end do
end program

The output is:

5    10
8    16
11   22
14   28
17   34
20   40

This time, integer variable is num and it ranges from 5 to 20 but with the step of 3. So num value starts with 5 and changes by 3 unit until it reaches 20. Therefore, num value will be, 5, 8, 11, 14, 17 and 20.

The print*, num, 2*num statement prints out num value and two times of num value until num value exceeds 20.

Now let’s not do any calculation but execute same statement about 6 times.

program new
   implicit none
      integer :: x
         do x = 5, 11
           print*, 'hello'
           print*, 'hi'
         end do
end program

See the Output:

hello
hi
hello
hi
hello
hi
hello
hi
hello
hi
hello
hi

This time x value is from 5 to 11 that means 6 times. So the two PRINT STATEMENTS inside DO loop is executed 6 times printing out 12 lines in the output. You can change x value range as your choice but just be sure that you provide the right calculated number for the required number of repetitions.

Examples of DO Loop in FORTRAN

Fortran Program to find sum of natural number

1. Write a program to calculate the sum of natural number.
program total
   implicit none
     integer :: sum, i
     sum = 0
       do i = 1, 10
         sum = sum + i
       end do
       print*, sum
end program

The output is:

55

If you see the highlighted line, that’s the main calculation statement which is going to repeat 10 times. First, we set sum = 0 then we do sum = sum + i that means we add i to sum. But i value changes from 1 to 10 on each repition, so that we can add every numbers from 1 to 10 using 10 repitions.

Now, try to add code so that the user has to enter the number up to which, the sum to be calculated of.

Fortran Program to find a factor of any number

2. Write a program to calculate the factor of any number.
program factor
  implicit none
  integer :: x
  integer :: fact
   fact = 1
    do x = 1, 5
      fact = fact * x
    end do
    print*, fact
end program

The output is:

120

This factor program is similar to above program of finding a sum but only difference is that for the sum we set 0 at first but for the factor we set 1 at first. Do you know why? Find.

General Do Loop

There is one more way of writing a loop in FORTRAN and that is general DO LOOP. So what’s the difference between the previous one and this one?

The only difference is this general DO LOOP doesn’t need starting and ending point. So it’s better use is for infinite loop.

let’s see one example

program infinite
  implicit none
   integer :: i = 0
	do
	  print*, i
	  print*, i + 1
	end do
end program

This program prints out the numbers starting from 0 and never ends. This might create a problem in system. To stop this program, press Ctrl+C and the execution stops.

There are other ways to stop the infinite loop using loop control elements which are to be discussed in next tutorial.

We can use GOTO statement instead of general DO loop if we not sure when to stop the loop. And remember try to avoid this general DO LOOP as we have other good ways too for the loop.

Conclusion and Summary

This DO LOOP is the best way to perform repetitions and is always used to solve physics and various other problems. But before you go, remember these key notes:

  • You have to declare one integer variable to write loop code.
  • Try to avoid infinite loop as possible.
  • First try loop for few repetitions and then add stopping point for more repetitions.

If you feel okay with this lesson of FORTRAN DO LOOP, go to the next lesson, but if you are not confident enough, then I suggest you watch the video for visual details.

Share this post:

Leave a Comment

Your email address will not be published. Required fields are marked *