MATLAB - Continue Statement
MATLAB - The continue Statement
Section titled “MATLAB - The continue Statement”The continue statement is a control flow tool used inside for or while loops. When MATLAB executes a continue statement, it immediately stops processing the current loop iteration and proceeds to the next one, skipping any remaining commands within the loop’s body for that iteration.
In essence, continue says: “I’m done with this specific iteration, please move on to the next one.”
How It Works: Control Flow
Section titled “How It Works: Control Flow”The continue statement alters the normal flow of a loop. Here’s a step-by-step description of what happens:
- The loop begins an iteration.
- Code inside the loop executes sequentially.
- An
ifcondition is met that triggers acontinuestatement. - Action: MATLAB immediately stops executing any further code within the current iteration.
- Control jumps back to the top of the loop.
- The loop’s counter (in a
forloop) is updated, or the condition (in awhileloop) is re-evaluated. - If the loop condition is still true, the next iteration begins. Otherwise, the loop terminates.
Practical Example: Summing Only Positive Numbers
Section titled “Practical Example: Summing Only Positive Numbers”Let’s create a script to sum only the positive numbers in a data vector, skipping any negative numbers or zeros.
% A vector with mixed positive and negative valuesdata = [10, 25, -5, 8, -12, 30];
totalSum = 0;
% Loop through each element of the data vectorfor i = 1:length(data) currentValue = data(i);
% Check if the value is not positive if currentValue <= 0 fprintf('Skipping non-positive value: %d\n', currentValue); continue; % Skip the rest of this iteration and go to the next end
% This part only runs if the 'continue' was not triggered fprintf('Adding value: %d\n', currentValue); totalSum = totalSum + currentValue;end
fprintf('\nFinal Sum of Positive Numbers: %d\n', totalSum);When you run this script, it produces the following result:
Adding value: 10Adding value: 25Skipping non-positive value: -5Adding value: 8Skipping non-positive value: -12Adding value: 30
Final Sum of Positive Numbers: 73Notice how the iteration for -5 and -12 printed the “Skipping” message and then immediately continued to the next element, bypassing the addition step.
Comparing continue with break and if-else
Section titled “Comparing continue with break and if-else”continuevs.break:continueskips one iteration and stays within the loop.breakterminates the entire loop immediately.continuevs.if-else: You can often achieve the same logic with a nestedifstatement. However,continuecan make your code more readable by reducing nesting depth, especially in complex loops. It helps to flatten the logic by handling special cases at the top of the loop.
Alternative using if-else
Section titled “Alternative using if-else”% Same logic using an if-else blocktotalSum = 0;for i = 1:length(data) currentValue = data(i); if currentValue > 0 % All processing logic is nested inside this 'if' block fprintf('Adding value: %d\n', currentValue); totalSum = totalSum + currentValue; else fprintf('Skipping non-positive value: %d\n', currentValue); endendfprintf('\nFinal Sum of Positive Numbers: %d\n', totalSum);For simple cases, the choice is a matter of style. For loops with many conditions and processing steps, using continue to handle exceptions at the beginning can make the main processing logic cleaner and less indented.