print and println@printftry-catch for Exception HandlingfinallyWhile for and while loops are excellent for repeating blocks of code, sometimes you need to alter their standard execution flow. Julia provides two statements, break and continue, that offer this finer level of control within your loops. These statements allow you to exit a loop prematurely or skip a particular iteration, respectively, based on conditions evaluated inside the loop.
breakThe break statement immediately terminates the innermost loop (either for or while) in which it is encountered. Once break is executed, the program's control jumps to the first statement following the terminated loop. This is particularly useful when you're searching for something and want to stop as soon as it's found, or when a certain condition makes further iterations unnecessary or erroneous.
Consider a scenario where you're iterating through a list of numbers to find the first number that meets a specific criterion. Once found, continuing to check the remaining numbers would be inefficient.
println("Looking for the first number greater than 5:")
numbers = [1, 3, 7, 2, 8, 4]
found_item = nothing # Use 'nothing' to indicate no item found yet
for num in numbers
println("Checking: ", num)
if num > 5
found_item = num
println("Found ", num, "! Breaking loop.")
break # Exit the loop now
end
println(num, " is not greater than 5.")
end
if found_item !== nothing
println("The first number greater than 5 is: ", found_item)
else
println("No number greater than 5 was found.")
end
In this example, when num becomes 7, the condition num > 5 is true. found_item is updated, a message is printed, and break is executed. The loop terminates immediately, and the println(num, " is not greater than 5.") line for 7 is skipped, as are all subsequent iterations for 2, 8, and 4. The program then proceeds to the if found_item !== nothing check after the loop.
The break statement is a way to make your loops more efficient by avoiding unnecessary work.
Flow of execution with a
breakstatement inside a loop. If thebreakcondition is met, the loop terminates.
continueThe continue statement skips the rest of the current iteration of the loop and immediately proceeds to the next iteration. Unlike break, continue doesn't terminate the loop entirely. It's useful when you encounter a specific case within an iteration that you want to ignore or handle differently, without affecting subsequent iterations.
Imagine you want to sum only the positive numbers in a list, ignoring any negative numbers or zeros.
println("Summing only positive numbers:")
data = [10, -2, 5, 0, -8, 12]
sum_of_positives = 0
for x in data
print("Processing: ", x)
if x <= 0
println("... skipping (not positive).")
continue # Skip the rest of this iteration, go to the next x
end
sum_of_positives += x
println("... added. Current sum: ", sum_of_positives)
end
println("Total sum of positive numbers: ", sum_of_positives)
In this code, when x is -2, 0, or -8, the condition x <= 0 is true. The println message indicates a skip, and continue is executed. This causes the program to immediately jump to the next value in data (e.g., from -2 to 5). The lines sum_of_positives += x and the subsequent println are not executed for these non-positive numbers. The loop continues until all items in data have been processed.
The continue statement helps in streamlining loop bodies by allowing you to bypass specific parts of the logic for certain iterations.
Flow of execution with a
continuestatement. If thecontinuecondition is met, the remainder of the current iteration is skipped, and the loop proceeds to the next iteration.
Both break and continue are powerful tools for managing loop execution. However, use them judiciously. Overuse, especially in complex nested loops, can sometimes make code harder to follow. Often, restructuring the loop condition or using more if-elseif-else blocks can lead to clearer code. Nevertheless, when used appropriately, break and continue can make your code more concise and efficient by directly expressing the desired control flow.
Was this section helpful?
break and continue.© 2026 ApX Machine LearningEngineered with