Updated July 3, 2023
Introduction to For Loop in Matlab
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 checks for desired conditions and then executes a block of code repeatedly. The code block is implemented as long as those defined conditions are met. An explicit loop counter distinguishes the ‘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 will learn about For Loop in Matlab.
Syntax of For Loop:
for index = value/values
statement
end
Now let us understand the ‘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 the defined “step” value or decrease the value by “step” for negative values.
- valArray: Each iteration 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 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. The following example will print the ‘END’ value without a break after every iteration.
Code:
for I = eye (3)
disp(‘Value:’)
disp(I)
disp(‘END’)
end
This is how our output will look like:
Output:
Using it with a break statement 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 ended after the first iteration.
Conclusion
- To exit the loop using code, we can make use of a break
- If we want to skip forward coming instructions in ‘for loop’ & begin the next iteration, we can make use of continue
- If we iterate over single-column vector values, we can first take its transpose to create a row vector.
Recommended Articles
This is a guide to For Loop in Matlab. Here we discuss For Loop in Matlab, appropriate syntax, and respective examples. You may also have a look at the following articles to learn more –