Skip to main content
Engineering LibreTexts

9.4: Conditional Controlled Looping

  • Page ID
    54282
  • \( \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 conditional controlled loop repeats a series of one or more Fortran statements based on a condition. As such, the loop may execute an indeterminate number of times.

    One form of the conditional loop is:

    do while (conditional expression)
        <fortran statement(s)>
    end do
    

    In this form, the conditional expression is re-checked at the top of the loop on each iteration.

    A more general format of the conditional loop is:

    do
        <fortran statement(s)>
    end do
    

    As is, this loop will continue forever. Probably not so good. A selection statement, such as an IF statement, and an exit statement would be used to provide a means to terminate the looping.

    For example,

    do
        <fortran statement(s)>
        if (conditional expression) exit
        <fortran statement(s)>
    end do
    

    Would stop looping only when the conditional expression evaluates to true. The exit statement can be used multiple times in different locations as needed. An IF statement, in any form, can be used for either the exit or cycle statements.

    For example, a conditional loop could be used to request input from the user and keep re-prompting until the input is correct.

    integer :: month
    
    do
        write (*,*) "Enter month (1-12): "
        read (*,*) month
        if (month >= 1 .and. month <= 12) exit
        write (*,*) "Error, month must be between 1 and 12."
        write (*,*) "Please re-enter."
    end do
    

    This will keep re-prompting an unlimited number of times until the correct input (a number between 1 and 12) is entered.

    Since a counter controlled DO loop requires an integer loop counter, another use of conditional loops would be to simulate a real counter. For example, to display the values from 1.5 to 4.5 stepping by 0.25, the following conditional loop could be used.

    real :: currValue = 1.5
    
    do while (currValue <= 4.5)
        write (*,*) "Value = ", currValue
        currValue = currValue + 0.25
    end do
    

    The values of currValue and amount of the increment, set as 0.25, can be adjusted as needed.


    This page titled 9.4: Conditional 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.