| by Arround The Web | No comments

How to Do a for end Loop in MATLAB?

The “for loop” is a conditional iterative expression supported by many programming languages. It consecutively runs a section of code after checking for the specified conditions. As long as the predefined conditions are satisfied, the code block executes. Sometimes, we must terminate the loop whenever a specific condition satisfies. In such a case, the use of the break statement is an ideal solution.

The main objective of writing this article is to teach you how to terminate a for-end loop using a break statement.

What is the break Statement in MATLAB?

The break statement in MATLAB is used to terminate the for or while loop execution before the looping condition expires. The break statement takes the loop control out of the loop whenever a given condition satisfies. We use the keyword break for defining the break statement.

The syntax for break Statement
The syntax for using the break statement in for end loop is as follows:

break

How to Use the break Statement in MATLAB to do a for-end Loop

The use of for loop in MATLAB is simple, and the syntax of the for loop is given below:

for variable = startValue : increment : endValue
    % Place code here
end

The user has to define the startValue and endValue for the loop, while increment defines the step size by which the loop is incremented. If you don’t use the increment option, the default value for the step size is 1.

The following simple example illustrates the working of for loop in MATLAB that displays values from 1 to 20 with a step size of 1.

for i = 1:20
    disp(i)
end

The above code displays the values from 1 to 20, however, if you want to stop the execution of code at a certain value, you can use the break statement into the for loop. The provided simple MATLAB code shows how the break statement terminates the for loop.

sum=0;
for i=1:20
    sum=sum+i;
  if sum>15
      break
  end
  disp(sum)
end

In the given example, the for-end loop has 20 iterations but due to the specified condition, the break statement is used to terminate the loop after the 5th iteration as shown in the output.

Example 2
Let’s consider another example in which we create a vector having 10 elements. Then we use the for loop for printing the vector elements. We use the break statement with a condition vect(i)==5 and when the condition satisfies the for loop is terminated.

vect =1:10;
n=length(vect);
for i=1:n
    if vect(i)==5
break
    end
    disp(vect(i))
end
disp('program encounters the number 5')
disp(['at index no.:',num2str(i)])

In the above MATLAB code, when the specified condition vect(i)==5 is satisfied, the break statement takes the control out of the loop and terminates the for loop; however, the loop iterations are not completed yet.

In this way, you can utilize the break statement for terminating the for-end loop in MATLAB.

Conclusion

In MATLAB, The for loop is used to consecutively run a section of code until a specified number of iterations are completed. But, sometimes we have to terminate a loop whenever a given condition satisfies. For this, using the break statement is the best solution. The break statement ends the for loop and takes the loop control out of the loop whenever a specified condition is met. This tutorial will help users learn the use of a break statement in the for loop in MATLAB with the help of some simple examples.

Share Button

Source: linuxhint.com

Leave a Reply