From the course: Introduction to Fortran

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Looping constructs

Looping constructs

- [Instructor] Loops are how we can have sections of our code execute repeatedly. There are a couple of forms of loop in Fortran so let's take a look at them. Here's the syntax for a loop with a loop index variable. Note that the increment is optional and will be one if not specified. The loop index variable must be an integer and so must the expressions for start, end and increment. The loop index is assigned the value of start. Then it is checked whether it has a value greater than end. Or if the increment is negative if it is less than end. If it does, execution moves to the statement after the end do statement. If not, the statements in the loop are executed. Then the increment value is added to the index and the check is repeated. Note that the values for start, end, and increment are evaluated only once, thus once a loop begins execution, it is not possible to change how many times it will execute. Here's the…

Contents