continue - Pass control to next iteration of for or while loop - MATLAB (original) (raw)
Main Content
Pass control to next iteration of for
or while
loop
Syntax
Description
continue
passes control to the next iteration of a for
or while
loop. It skips any remaining statements in the body of the loop for the current iteration. The program continues execution from the next iteration.
continue
applies only to the body of the loop where it is called. In nested loops, continue
skips remaining statements only in the body of the loop in which it occurs.
Examples
Selectively Display Values in Loop
Display the multiples of 7 from 1 through 50. If a number is not divisible by 7, use continue
to skip the disp
statement and pass control to the next iteration of the for
loop.
for n = 1:50 if mod(n,7) continue end disp(['Divisible by 7: ' num2str(n)]) end
Divisible by 7: 7 Divisible by 7: 14 Divisible by 7: 21 Divisible by 7: 28 Divisible by 7: 35 Divisible by 7: 42 Divisible by 7: 49
Skip to Next Loop Iteration
Count the number of lines of code in the file magic.m
. Skip blank lines and comments using a continue
statement. continue
skips the remaining instructions in the while
loop and begins the next iteration.
fid = fopen('magic.m','r'); count = 0; while ~feof(fid) line = fgetl(fid); if isempty(line) || strncmp(line,'%',1) || ~ischar(line) continue end count = count + 1; end count
Tips
- The
continue
statement skips the rest of the instructions in afor
orwhile
loop and begins the next iteration. To exit the loop completely, use abreak
statement. continue
is not defined outside afor
orwhile
loop. To exit a function, usereturn
.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Version History
Introduced before R2006a