Skip to main content
Engineering LibreTexts

9.1: Counter Controlled Looping

  • Page ID
    54279
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    A counter controlled loop repeats a series of one or more Fortran statements a set number of times. The general format of the counting loop is:

    do count_variable = start, stop, step
        <fortran statement(s)>
    end do
    

    where the count variable must be an integer variable, start, stop, and step are integer variables or integer expressions. The step value is optional. If it is omitted, the default value is 1. If used, the step value cannot be zero. The <fortran statement(s)> is a sequence of statements and is referred to as the body of the do-loop. You can use any executable statement within a do-loop, including IF-THEN-ELSE-END IF and even another do-loop. Before the do-loop starts, the values of start, stop, and step are computed exactly once. More precisely, during the course of executing the do-loop, these values will not be re-computed.

    The count variable receives the value of start variable or expression. If the value of control-var is less than or equal to the value of stop-value, the <fortran statement(s)> part is executed. Then, the value of step (1 if omitted) is added to the value of control-var. At the end, the loop goes back to the top and compares the values of control-var and stop-value.

    If the value of control-var is greater than the value of final-value, the do-loop completes and the statement following end do is executed.

    For example, with the declarations,

    integer :: counter, init=1, final=10, sum=0
    

    the following do-loop,

    do counter = init, final
        sum = sum + counter
    end do
    write (*,*) "Sum is: ", sum
    

    will add the numbers between 1 and 10 which will result in 55. Since the step was not specified, it is defaulted 1.

    Another example, with the declarations,

    integer :: counter, init=1, final=10, step=2
    

    and the following do-loop,

    do counter = init, final, step
        write (*,*) counter
    end do
    

    will display the odd numbers between 1 and 10 (1, 3, 5, 7, 9).

    Another example would be to read some numbers from the user and compute the sum and average of those numbers. The example asks the user how many numbers, reads that many numbers, computes the sum, computes the average, and displays the results.

    program calcAverage
    implicit none
    integer :: count, number, sum, input
    real :: average
    
        write (*,*) "Enter count of numbers to read"
        read (*,*) count
    
        sum = 0
        do number = 1, count
            read (*,*) input
            sum = sum + input
        end do
    
        average = real(sum) / real(count)
        write (*,*) "Average = ", average
    
    end program calcAverage
    

    The use of the function real() converts the sum and count variables from integers to real values as required for the average calculation. Without this conversion, sum/count division would be interpreted as dividing an integer by an integer, yielding an integer result.

    A final example of a counter controlled loop is to compute the factorial of a positive integer. The factorial of an integer \(n\), written as \(n!\), is defined to be the product of 1, 2, 3, ..., \(n-1\), and \(n\). More precisely, \(n! = 1 \cdot 2 \cdot 3 \cdot \ldots \cdot n\).

    integer :: factorial, n, i
    
    factorial = 1
    do i = 1, n
        factorial = factorial * i
    end do
    

    The do-loop above iterates \(n\) times. The first iteration multiplies factorial with 1, the second iteration multiplies factorial with 2, the third time with 3, ..., the ith time with \(i\) and so on. Thus, the values that are multiplied with the initial value of factorial are 1, 2, 3, ..., \(n\). At the end of the do-loop, the value of factorial is \( 1 \cdot 2 \cdot 3 \cdot \ldots \cdot n\) which is \(n!\).


    This page titled 9.1: Counter Controlled Looping is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?