Documentation

continue

Pass control to next iteration of for or while loop

Syntax

  • continue

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

collapse all

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
fclose(fid);
count =

    31

More About

expand all

Tips

  • The continue statement skips the rest of the instructions in a for or while loop and begins the next iteration. To exit the loop completely, use a break statement.

  • continue is not defined outside a for or while loop. To exit a function, use return.

See Also

| |

Introduced before R2006a

Was this topic helpful?