English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The break statement terminates the execution of the for or while loop. Statements that appear after the break statement in the loop will not be executed.
In nested loops, the break only exits from the loop where the interruption occurred. Control is passed to the statement after the loop ends.
Create a script file and enter the following code-
a = 10; %The while loop is executed while(a < 20) fprintf('value of a: %d\n', a); a = a + 1; if(a > 15) %Use the break statement to terminate the loop break; end endWhen the file is run, it displays the following result-
value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15