Home » All Programs » ‘break’ Command In Loops In MatLab Programming

‘break’ Command In Loops In MatLab Programming

‘break’ command is a very useful command in Matlab programing in certain situations. In here, we explain the importance of ‘break’ command in Matlab with various examples.

What Is ‘break’ Command In MatLab?

To understand the ‘break’ command, we strongly recommend you to look at the ‘for-end’ loop in Matlab and how to use it. Take a look at the below example to understand ‘break’ command in Matlab.

>>x = [1 3 6; 1 6 1; 0 3 3]; 
for y = 1:size(x:1) %loop up to row number of x,
    for z = 1:size(x:2) %loop up to column number of x,
        if A(y,x) == 0 %if query to each matrix element

row = 3

column = 1

At above code inside command window of Matlab, we want to obtain a code that finds the first element that zero in a matrix. This program stops when it finds the 0 element inside matrix.

We created our matrix ‘x’ in which the 3×1 element of it matrix is 0. To find the place of this element inside matrix ‘x’, we created for loops that are intertwined to each other. In outermost for loop, we assigned variable ‘y’ which will take the row numbers starting from 1 in each loop, up to the row number of ‘x’ is reached. This is done with ‘size()’ command in Matlab.

At the innermost for loop, we assigned variable ‘z’ to index the each column of ‘x’ matrix at each loop steps.

Inside the inner for loop, we put a if-else query to find out the matrix element that is 0. In this query, we asking the equality of each matrix element to ‘0’ in each loop.

YOU CAN LEARN MatLab IN MECHANICAL BASE; Click And Start To Learn MatLab!

When the query will take the response ‘true’, ‘break’ command will stop the for-end loop to halt the code. Then you will see the ‘row’ and ‘column’ results of first element that is ‘0’ at command window.

The use of ‘break’ command is very simple as you see, and it is very useful to halt the code immediately, if the statement is provided in Matlab code.

Do not forget to leave your comments and questions about the ‘break’ command in Matlab below. Your precious feedbacks are very important for us.


Leave a Reply

Your email address will not be published. Required fields are marked *


leave feedback ?

( For the post )



We are always open to your feedback to improve ourselves and the quality of our content! If you have any suggestions, thoughts, or criticism, please let us know. We are trying to improve our blog with constructive feedback. We are aware of how valuable your feedback is for our future development, and we will carefully read all your comments. Thank you in advance!