break - Terminate execution of for or while loop - MATLAB (original) (raw)

Main Content

Terminate execution of for or while loop

Syntax

Description

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.

example

Examples

collapse all

Exit Loop Before Expression Is False

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 break end s = s + tmp; end

Tips

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