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]
labelspecifies 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
iequals 5, theexitstatement 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
iequals 3, thecyclestatement 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
ireaches 5, theexitstatement 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 innerskips the current iteration of the inner loop.exit outerbreaks 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,
exitstops 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
valuebecomes 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:
cycleskips the iteration wheni=3.exitterminates the loop wheni=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 > 7 .or. i == 4) exit
if (i == 2 .or. i == 5) cycle
print *, i
end do
Explanation:
- Loop exits when
i > 7ori == 4. - Skips iteration when
i == 2ori == 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 > 0) exit
print *, "Invalid input, try again."
end do
print *, "You entered:", value
Explanation:
- The loop continues until the user enters a valid number.
exitbreaks 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) < 0) cycle
if (sensorData(i) > 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
- Use EXIT sparingly: Only when necessary to avoid confusing program flow.
- Use CYCLE for skipping iterations: Makes loops more readable.
- Label loops in nested cases: Improves clarity in EXIT/CYCLE usage.
- Avoid deep nesting: Can make control logic hard to follow.
- Document the purpose: Clearly comment why EXIT or CYCLE is used.
12. Common Pitfalls
- Unintended early exit: Ensure the EXIT condition is precise.
- Skipping iterations incorrectly: Misusing CYCLE may skip important computations.
- Confusing labels in nested loops: Can cause EXIT/CYCLE to affect the wrong loop.
- 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
DOandDO WHILEloops. - 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.
Leave a Reply