MATLAB - Break Statement Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Break statement in MATLAB is used for breaking out of an iterative loop, for or while loop. The break in MATLAB is similar to the break statements in other programming languages such as C, C++, Python, etc. We will see how to break out of a single for or while loop and the nested implementation of the same. Syntax:For loopfor <iteration condition> statement 1 -------------- break conditional break end While loopwhile <condition> statement 1 -------------- break conditional break end We will use a loop that returns the first complete divisor of a number in the specified range. Example 1: S Output: We will use the same case with the while loop to see the usage of a break in the while loop. Example 2: Matlab % MATLAB code for break in the while loop. % Number whose first divisor is to be calculated num = 35; i=2; %starting the while loop while true rem = mod(num,i); % Condition for break if(rem==0) break end %incrementing i i=i+1; end % Displaying the divisor disp(i) Output: Using the break statement with nested loops. We will find the index of number 23 in magic square of 5x5 using nested loops. Example 3: Matlab % MATLAB code for update arr = magic(5); index=[-1 -1]; % Finding if 23 exits in arr and returning the index else, return -1 for i=1:5 for j=1:5 % Checks if arr(i,j) is 23 if(arr(i,j) == 23) index = [i j]; break % Breaks the inner loop if 23 is found end end % Checks if value of row is changed if(index(1)~=-1) % Breaks the outer loop if row is not -1 break end end % Displays the index of 23 in magic(5) disp("index is = ") disp(index) Output: As can be seen, 23 is located in the 1st column of the 2nd row thus, the index is 2,1. Comment More infoAdvertise with us Next Article MATLAB - Break Statement O owl0223 Follow Improve Article Tags : Software Engineering MATLAB-programs Similar Reads MATLAB - Conditional Statements Conditional statements are something that is very basic and important for every programmer. There will be some situations where a program or a particular block has to be executed only when a specific condition is True. These conditional statements will be very handy and fruitful in such situations. 6 min read Swift - Break Statement The break statement is a loop control statement that is used to end the execution of an entire control flow statement immediately when it is encountered. When the break condition is true, the loop stops its iterations, and control returns immediately from the loop to the first statement after the lo 6 min read Break and Next statements in R In R Programming Language, we require a control structure to run a block of code multiple times. Loops come in the class of the most fundamental and strong programming concepts. A loop is a control statement that allows multiple executions of a statement or a set of statements. The word âloopingâ me 3 min read R Next Statement Next statement in R is used to skip any remaining statements in the loop and continue the execution of the program. In other words, it is a statement that skips the current iteration without loop termination. 'next' is a loop control statement just like the break statement. But 'next' statement wor 6 min read R - if statement If statement is one of the Decision-making statements in the R programming language. It is one of the easiest decision-making statements. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is ex 3 min read Like