Skip to content

MATLAB - 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.”

The continue statement alters the normal flow of a loop. Here’s a step-by-step description of what happens:

  1. The loop begins an iteration.
  2. Code inside the loop executes sequentially.
  3. An if condition is met that triggers a continue statement.
  4. Action: MATLAB immediately stops executing any further code within the current iteration.
  5. Control jumps back to the top of the loop.
  6. The loop’s counter (in a for loop) is updated, or the condition (in a while loop) is re-evaluated.
  7. 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 values
data = [10, 25, -5, 8, -12, 30];
totalSum = 0;
% Loop through each element of the data vector
for 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: 10
Adding value: 25
Skipping non-positive value: -5
Adding value: 8
Skipping non-positive value: -12
Adding value: 30
Final Sum of Positive Numbers: 73

Notice how the iteration for -5 and -12 printed the “Skipping” message and then immediately continued to the next element, bypassing the addition step.

  • continue vs. break: continue skips one iteration and stays within the loop. break terminates the entire loop immediately.
  • continue vs. if-else: You can often achieve the same logic with a nested if statement. However, continue can 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.
% Same logic using an if-else block
totalSum = 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);
end
end
fprintf('\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.