Documentation

continue

Pass control to next iteration offororwhileloop

Syntax

Description

example

continuepasses control to the next iteration of afororwhileloop. It skips any remaining statements in the body of the loop for the current iteration. The program continues execution from the next iteration.

continueapplies only to the body of the loop where it is called. In nested loops,continueskips remaining statements only in the body of the loop in which it occurs.

Examples

collapse all

Display the multiples of 7 from 1 through 50. If a number is not divisible by 7, usecontinueto skip thedispstatement and pass control to the next iteration of theforloop.

forn = 1:50ifmod(n,7)continueenddisp(['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

Count the number of lines of code in the filemagic.m. Skip blank lines and comments using acontinuestatement.continueskips the remaining instructions in thewhileloop and begins the next iteration.

fid = fopen('magic.m','r'); count = 0;while~feof(fid) line = fgetl(fid);ifisempty(line) || strncmp(line,'%',1) || ~ischar(line)continueendcount = count + 1;endcount
count = 31
fclose(fid);

Tips

  • Thecontinuestatement skips the rest of the instructions in afororwhileloop and begins the next iteration. To exit the loop completely, use abreakstatement.

  • continueis not defined outside afororwhileloop. To exit a function, usereturn.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

See Also

||

Introduced before R2006a