Exiting Loops in Fortran EXIT and CYCLE

Loops are fundamental programming structures used to execute a block of code repeatedly. In Fortran, controlling the flow inside loops is crucial to ensure programs behave as intended. The EXIT and CYCLE statements allow precise control by either terminating a loop prematurely or skipping the current iteration, respectively. Understanding these commands is key to writing efficient and readable Fortran programs.

1. Introduction to Loop Control

In programming, loops are used for repetitive tasks such as:

  • Iterating through arrays
  • Performing calculations multiple times
  • Processing input or data sequences

Fortran provides multiple types of loops, including DO, DO WHILE, and nested loops. However, sometimes it is necessary to interrupt the normal flow of a loop.

EXIT and CYCLE provide mechanisms for:

  • EXIT – Breaking out of a loop before its natural termination
  • CYCLE – Skipping the current iteration and moving to the next

2. The EXIT Statement

The EXIT statement allows the program to immediately terminate a loop, even if the loop has not reached its ending condition.

Syntax:

exit

Optional form with loop label:

exit [label]
  • label specifies the loop to exit in case of nested loops.

Basic Example of EXIT

integer :: i
do i = 1, 10
if (i == 5) exit
print *, i
end do

Explanation:

  • The loop iterates from 1 to 10.
  • When i equals 5, the exit statement breaks the loop.
  • Only numbers 1 to 4 are printed.

Output:

1
2
3
4

3. The CYCLE Statement

The CYCLE statement skips the remaining statements in the current iteration of a loop and moves directly to the next iteration.

Syntax:

cycle

Optional form with loop label:

cycle [label]
  • Useful in nested loops to skip iterations of a specific loop.

Basic Example of CYCLE

integer :: i
do i = 1, 5
if (i == 3) cycle
print *, i
end do

Explanation:

  • When i equals 3, the cycle statement skips the print statement.
  • Iterations 1, 2, 4, and 5 execute normally.

Output:

1
2
4
5

4. Combining EXIT and CYCLE

EXIT and CYCLE can be used together to control loops precisely.

integer :: i
do i = 1, 10
if (i == 5) exit
if (i == 3) cycle
print *, i
end do

Explanation:

  • Iteration 3 is skipped by cycle.
  • When i reaches 5, the exit statement terminates the loop.

Output:

1
2
4

5. EXIT and CYCLE in Nested Loops

For nested loops, EXIT and CYCLE can specify which loop to affect using labels.

integer :: i, j
outer: do i = 1, 3
inner: do j = 1, 5
    if (j == 3) cycle inner
    if (j == 4) exit outer
    print *, 'i=', i, 'j=', j
end do inner
end do outer

Explanation:

  • cycle inner skips the current iteration of the inner loop.
  • exit outer breaks out of the outer loop completely.
  • Output:
i= 1 j= 1
i= 1 j= 2
i= 1 j= 5

6. Practical Examples of EXIT

6.1 Searching in an Array

integer, dimension(5) :: arr = [10, 20, 30, 40, 50]
integer :: i, target
target = 30

do i = 1, 5
if (arr(i) == target) then
    print *, "Target found at index", i
    exit
end if
end do

Explanation:

  • The loop searches for a target value.
  • Once found, exit stops further iterations.

Output:

Target found at index 3

6.2 Early Termination in Calculations

real :: sum, value
integer :: i
sum = 0.0

do i = 1, 10
value = 10.0 / i
if (value < 1.0) exit
sum = sum + value
end do print *, "Sum =", sum
  • The loop stops adding when value becomes less than 1.

7. Practical Examples of CYCLE

7.1 Skipping Invalid Data

integer, dimension(6) :: data = [2, -1, 5, -3, 4, 0]
integer :: i

do i = 1, 6
if (data(i) < 0) cycle
print *, "Processing", data(i)
end do

Explanation:

  • Negative numbers are skipped using cycle.
  • Only non-negative numbers are processed.

Output:

Processing 2
Processing 5
Processing 4
Processing 0

7.2 Skipping Specific Iterations

integer :: i
do i = 1, 10
if (mod(i, 2) == 0) cycle
print *, i
end do

Explanation:

  • Skips even numbers using cycle.
  • Only odd numbers are printed.

Output:

1
3
5
7
9

8. EXIT and CYCLE in DO WHILE Loops

Fortran also allows EXIT and CYCLE in DO WHILE loops.

integer :: i
i = 0

do while (i < 10)
i = i + 1
if (i == 5) exit
if (i == 3) cycle
print *, i
end do

Explanation:

  • cycle skips the iteration when i=3.
  • exit terminates the loop when i=5.

Output:

1
2
4

9. Combining with Logical Conditions

EXIT and CYCLE can be combined with logical operators for advanced control:

integer :: i
do i = 1, 10
if (i &gt; 7 .or. i == 4) exit
if (i == 2 .or. i == 5) cycle
print *, i
end do

Explanation:

  • Loop exits when i > 7 or i == 4.
  • Skips iteration when i == 2 or i == 5.

Output:

1
3

10. Use Cases in Real Programs

10.1 Input Validation

integer :: value
do
print *, "Enter a positive number:"
read *, value
if (value &gt; 0) exit
print *, "Invalid input, try again."
end do print *, "You entered:", value

Explanation:

  • The loop continues until the user enters a valid number.
  • exit breaks the loop once the condition is satisfied.

10.2 Processing Sensor Data

integer :: i
real, dimension(10) :: sensorData = [1.2, 3.4, -1.0, 2.5, 0.0, 4.6, 5.1, -2.3, 3.3, 1.0]

do i = 1, 10
if (sensorData(i) &lt; 0) cycle
if (sensorData(i) &gt; 5.0) exit
print *, "Sensor reading:", sensorData(i)
end do

Explanation:

  • Skips invalid negative readings.
  • Stops if a reading exceeds 5.0.
  • Ensures clean and safe processing of data streams.

11. Best Practices

  1. Use EXIT sparingly: Only when necessary to avoid confusing program flow.
  2. Use CYCLE for skipping iterations: Makes loops more readable.
  3. Label loops in nested cases: Improves clarity in EXIT/CYCLE usage.
  4. Avoid deep nesting: Can make control logic hard to follow.
  5. Document the purpose: Clearly comment why EXIT or CYCLE is used.

12. Common Pitfalls

  1. Unintended early exit: Ensure the EXIT condition is precise.
  2. Skipping iterations incorrectly: Misusing CYCLE may skip important computations.
  3. Confusing labels in nested loops: Can cause EXIT/CYCLE to affect the wrong loop.
  4. Infinite loops: If EXIT conditions are never met in DO WHILE loops.

13. Summary

  • EXIT: Terminates a loop prematurely.
  • CYCLE: Skips the current iteration and continues.
  • Both statements work in DO and DO WHILE loops.
  • Can be used with logical conditions and in nested loops.
  • Essential for input validation, data processing, and efficient loop control.
  • Proper use improves readability and maintains robust program behavior.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *