Octave for Statement [ 2024 ]
Octave for Statement
In this tutorial, we will learn about Octave for Statement. The for statement is a loop statement.
for Statement Syntax
The for statement is used to execute a set of statements for a fixed number of iterations. for loop is a definite loop. We can use this loop if we know the number of iterations beforehand.
The syntax of the for statement is:
for var = expression
for_loop_body
endfor
or
for var = expression
for_loop_body
end
For better readability we can use the endfor to end the loop.
Simple Example
% Sample Octave Script
% For Loop Demo
% Octave Tutorials – www.TestingDocs.com
%Clear environment
clc;
clear;
% for Loop to print 1 -> 10
for n= 1 : 10
fprintf(‘For Loop Iteration = %d \n’,n);
endfor
The for loop runs for ten iterations and prints the iteration number.
Output
Nested for loops
In Octave, it is possible to write a nested for loop, that is, a for loop within another for loop.
For example, two nested for loops.
for i = 1:n
for j = 1:m
% Inner for loop statements
end % Inner for loop
% Some outer for loop statements
end % Outer for loop
Octave Tutorials
Octave Tutorial on this website can be found at:
More information on Octave can be found on the official website: