MATLAB provides its user with a basket of functions, in this article we will understand a powerful element called ‘For loop’. For loop is a conditional iterative statement used in programming languages. It is used to check for desired conditions and then executes a block of code repeatedly. The block of code is implemented as long as those defined conditions are met. An explicit loop counter is used to distinguish ‘for loop’ from other looping statements. This is also referred to as the loop variable, this allows the loop body to know the sequencing of every iteration. In this topic, we are going to learn about For Loop in Matlab.
Syntax of For Loop:
for index = value/values
statement
end
Now let us understand ‘for loop’ in detail.
Examples of For Loop in Matlab
For index = It will include values, single or multiple statements, and end
This function will run a defined set of statements in the loop for the number of times specified in the condition
Values can have a number of forms e.g:
- firstVal: lastVal: it will gradually increase the index by 1 from firstval till lastval, it will run the set of statements till firstVal is greater than the lastVal
- firstVal: step : lastVal: it will gradually increase the index by defined “step” value or it will decrease the value by “step” for negative values
- valArray: For each iteration, it will generate a column vector from columns of the array for valArray
Now let us use ‘for loop’ in some examples:
Decrement Values
It will decrement the values by the defined interval.
Code:
for a = 2.0 : -0.5 : 0.0
disp(a)
end
Here, our output will be decremented by ‘0.5’
Output:

Let’s take a different decrement interval
Code:
for a = 3.0 : -1.0 : 0.0
disp(a)
end
Here is the output that we will get, as we can notice, the values are decremented by 1:
Output:

Increment Values
It will increment the values by the defined interval.
Code:
for a = 6.0 : 1.0 : 9.0
disp(a)
end
Here is the output that we will get, as we can notice, the values are incremented by 1:

Let’s take a different decrement interval:
Code:
for a = 4.0 : 2.0 : 9.0
disp(a)
end
Here is the output that we will get, as we can see, the values are incremented by 2:
Output:

Specified Values
It will execute statements for specified values
Code:
for a = [1 3 8 7] disp(a)
end
This is how our output will look like:
Output:

As we can notice, the values are simply printed.
Use of Repeat Statement for every Matrix Column
Here the eye is a 2X3 Identity matrix
Code:
for I = eye (2,3)
disp('Current value:')
disp(I)
end
This is how our output will look like:
Output:

Use of BREAK Statement
To exit the ‘for loop’ we can use the break statement. Without break, the following example will print the ‘END’ value after every iteration
Code:
for I = eye (3)
disp(‘Value:’)
disp(I)
disp(‘END’)
end
This is how our output will look like:
Output:

If we use it with a break statement, it will break the ‘for loop’ after the first iteration.
Code:
for I = eye (3)
disp(‘Value:’)
disp(I)
break
disp(‘END’)
end
Output:

As we can notice, our loop is ended after the first iteration.