Skip to main content
Engineering LibreTexts

9: Looping

  • Page ID
    54209
  • \( \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}}\)

    When a series of Fortran statements need to be repeated, it is referred to as a loop or do-loop. A Fortran do-loop is a special control statement that allows a Fortran statement or set of statements to be executed multiple times. This repetition can be based on a set number of times, referred to as counter controlled, or based on a logical condition, referred to as conditionally controlled. Each of these looping methods is explained in the following sections.

    • 9.1: Counter Controlled Looping
      A counter controlled loop repeats a series of one or more Fortran statements a set number of times.
    • 9.2: EXIT and CYCLE Statements
      The exit and cycle statements are used to modify the execution of a do-loop. The exit statement is used to exit a loop. The exit can be used alone, but it is typically used with a conditional statement to allow exiting a loop based on a specific condition. The exit statement can be used in a counter controlled loop or a conditionally controlled loop.
    • 9.3: Counter Controlled Example
      In this example, we will write a Fortran program to find the difference between the sum of the squares and the square of the sum of the first N natural numbers.
    • 9.4: Conditional Controlled Looping
      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.
    • 9.5: Conditionally Controlled Loop Example
      In this example, we will write a Fortran program that will read a valid date from the user. The date will consist of three values, one for each of the month, day, and year. This example will use some of the previous example fragments.
    • 9.6: Exercises


    This page titled 9: 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?