For in Fortran
Fortran provides several looping constructs. Here are some basic types of loops.
To run the program, save it as for_example.f90
and compile it using a Fortran compiler, then run the executable:
In Fortran:
- The
do
statement is used for loops. do while
is used for condition-based loops.- A simple
do
with start and end values is used for counting loops. exit
is used to break out of a loop (equivalent to break
in other languages).cycle
is used to continue to the next iteration (equivalent to continue
in other languages).
We’ll see some other loop forms later when we look at array operations and other data structures.