Skip to content

MATLAB - Break Statement

MATLAB - Loop Control with break and continue

Section titled “MATLAB - Loop Control with break and continue”

In MATLAB, loop control statements change the standard execution flow of for or while loops. The two primary control statements are break and continue. Understanding how and when to use them is crucial for writing efficient and readable code.

The break statement completely terminates the execution of the innermost for or while loop in which it appears. Any statements in the loop that come after the break statement are not executed. Control immediately passes to the statement following the end of that loop.

+-----------------+
| Loop Entry |
+-------+---------+
|
+-------v---------+
| Condition Check |
+-------+---------+
| (True)
+-------v---------+
| Loop Body Stmts |
+-------+---------+
|
+-------v---------+
| if (break_cond) | --(True)--> To statement after loop
+-------+---------+
| (False)
+-------v---------+
| Remainder of |
| Loop Body |
+-------+---------+
|
+---------------------> Back to Condition Check

Example: Finding the First Value Above a Threshold

Section titled “Example: Finding the First Value Above a Threshold”

Imagine you are monitoring a data stream and need to stop as soon as a value exceeds 15. A while loop with a break statement is perfect for this.

Create a script file and type the following modern, well-commented code:

% Modern MATLAB script demonstrating the 'break' statement
a = 10; % Initialize starting value
% Loop as long as 'a' is less than 20
while a < 20
fprintf('Current value of a: %d\n', a);
% Check for the exit condition
if a > 15
disp('Threshold exceeded. Terminating loop with break.');
break; % Exit the loop immediately
end
a = a + 1; % Increment a
end
disp('Execution continues after the loop.');
fprintf('Final value of a is: %d\n', a);

When you run the file, it displays the following result:

Current value of a: 10
Current value of a: 11
Current value of a: 12
Current value of a: 13
Current value of a: 14
Current value of a: 15
Current value of a: 16
Threshold exceeded. Terminating loop with break.
Execution continues after the loop.
Final value of a is: 16

The continue statement, in contrast to break, skips the current iteration of a loop and passes control to the next iteration. Any statements in the loop body after the continue statement are skipped for that iteration only.

Let’s use a for loop to iterate through a vector of numbers but only process the odd ones, skipping the even numbers using continue.

% Modern MATLAB script demonstrating the 'continue' statement
data = [1, 4, 7, 8, 9, 10, 13];
fprintf('Processing odd numbers in the data vector...\n');
for k = 1:length(data)
currentValue = data(k);
% Check if the number is even. The mod() function gives the remainder.
if mod(currentValue, 2) == 0
fprintf('...Skipping even number: %d\n', currentValue);
continue; % Skip the rest of this iteration and start the next
end
% This part only executes for odd numbers
fprintf('Processing odd number: %d\n', currentValue);
end
disp('Loop finished.');

The output clearly shows the even numbers being skipped:

Processing odd numbers in the data vector...
Processing odd number: 1
...Skipping even number: 4
Processing odd number: 7
...Skipping even number: 8
Processing odd number: 9
...Skipping even number: 10
Processing odd number: 13
Loop finished.
  • Clarity is Key: break and continue can sometimes make code harder to follow. If your loop logic becomes very complex with multiple break or continue statements, consider refactoring your code into a function. A return statement in a function can often be a cleaner alternative to break.
  • Nested Loops: Remember that break and continue only affect the innermost loop they are in. They do not exit from nested loops.
  • break vs. continue: The most common mistake is confusing the two. Remember: break = exit the loop entirely. continue = skip the current iteration.