This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Terminate execution of for or while loop





break terminates the execution of a for or while loop. Statements in the loop after the break statement do not execute.

In nested loops, break exits only from the loop in which it occurs. Control passes to the statement that follows the end of that loop.


collapse all

Sum a sequence of random numbers until the next random number is greater than an upper limit. Then, exit the loop using a break statement.

limit = 0.8;
s = 0;

while 1
    tmp = rand;
    if tmp > limit
    s = s + tmp;


  • The break statement exits a for or while loop completely. To skip the rest of the instructions in the loop and begin the next iteration, use a continue statement.

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

Extended Capabilities

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

See Also

| | | |

Introduced before R2006a

Was this topic helpful?