This is machine translation

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

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

break, _break

Terminate a loop or a Case switch prematurely

MuPAD® notebooks are not recommended. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.




break terminates for, repeat, while loops, and case statements.

The break statement is equivalent to the function call _break(). The return value is the void object of type DOM_NULL.

Inside for, repeat, while, and case statements, the break statement exits from the loop/switch. Execution proceeds with the next statement after the end clause of the loop/switch.

In nested loops, only the innermost loop is terminated by break.

break also terminates a statement sequence _stmtseq(..., break, ...).

Outside for, repeat, while, case, and _stmtseq, the break statement has no effect.


Example 1

Loops are exited prematurely by break:

for i from 1 to 10 do
  if i = 2 then break end_if

delete i:

Example 2

In a case statement, all commands starting with the first matching branch are executed:

x := 2:
case x
 of 1 do print(1); x^2;
 of 2 do print(2); x^2;
 of 3 do print(3); x^2;
 otherwise print(UNKNOWN)

In the next version, break ensures that only the statements in the matching branch are evaluated:

case x
 of 1 do print(1); x^2; break;
 of 2 do print(2); x^2; break;
 of 3 do print(3); x^2; break;
 otherwise print(UNKNOWN)

delete x:

See Also

MuPAD Functions

Was this topic helpful?