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
Section titled “The break Statement”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.
Flow Diagram for break
Section titled “Flow Diagram for break” +-----------------+ | 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 CheckExample: 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 20while 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 aend
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: 10Current value of a: 11Current value of a: 12Current value of a: 13Current value of a: 14Current value of a: 15Current value of a: 16Threshold exceeded. Terminating loop with break.Execution continues after the loop.Final value of a is: 16The continue Statement
Section titled “The continue Statement”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.
Example: Processing Only Odd Numbers
Section titled “Example: Processing Only Odd Numbers”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: 4Processing odd number: 7...Skipping even number: 8Processing odd number: 9...Skipping even number: 10Processing odd number: 13Loop finished.Best Practices and Common Mistakes
Section titled “Best Practices and Common Mistakes”- Clarity is Key:
breakandcontinuecan sometimes make code harder to follow. If your loop logic becomes very complex with multiplebreakorcontinuestatements, consider refactoring your code into a function. Areturnstatement in a function can often be a cleaner alternative tobreak. - Nested Loops: Remember that
breakandcontinueonly affect the innermost loop they are in. They do not exit from nested loops. breakvs.continue: The most common mistake is confusing the two. Remember:break= exit the loop entirely.continue= skip the current iteration.