Loops (For and While) and Control Statements in Octave
Last Updated :
18 Aug, 2022
Control statements are expressions used to control the execution and flow of the program based on the conditions provided in the statements. These structures are used to make a decision after assessing the variable. In this article, we’ll discuss control statements like the if statement, for and while loops with examples.
if condition
This control structure checks the expression provided in parenthesis is true or not. If true, the execution of the statements continues. Syntax :
if (condition)
statements
...
...
end
(endif can also be used)
Example :
MATLAB
variable1 = 20;
variable2 = 20;
if variable1 == variable2,
disp( 'The variables are Equal' );
endif;
|
Output:
The variables are Equal
if-else condition
It is similar to if condition but when the test expression in if condition fails, then statements in else condition are executed. Syntax :
if (condition)
statements
...
...
else
statements
...
...
end
(endif can also be used)
Example :
MATLAB
variable1 = 20;
variable2 = 40;
if variable1 == variable2,
disp( 'The variables are Equal' );
else
disp( 'The variables are Not Equal' );
end ;
|
Output:
The variables are Not Equal
if-elseif condition
When the first if condition fails, we can use elseif to supply another if condition. Syntax :
if (condition)
statements
...
...
elseif (condition)
statements
...
...
else
statements
...
...
end
(endif can also be used)
Example :
MATLAB
var = 50;
if var < 50,
disp( 'The variable is less than 50' );
elseif var > 50,
disp( 'The variable is greater than 50' );
else
disp( 'The variable is 50' );
end ;
|
Output:
The variable is 50
for loop
It is a type of loop or sequence of statements executed repeatedly until exit condition is reached. Syntax :
for var = expression
body
end
(endfor can also be used)
Example 1 : Printing numbers from 1 to 5 :
MATLAB
for i = 1:5,
disp(i);
end ;
|
Output :
1
2
3
4
5
Example 2 : for loop with vectors :
MATLAB
v = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10];
for i = 1:10,
v(i) = v(i) * 10;
end ;
disp(v)
|
Output :
10
20
30
40
50
60
70
80
90
100
Example 4 : Program to print the Fibonacci series up to 10 elements using for loop :
MATLAB
fibonacci = ones(1, 10);
for i = 3:10
fibonacci(i) = fibonacci(i - 1) + fibonacci(i - 2);
endfor
disp(fibonacci)
|
Output :
1 1 2 3 5 8 13 21 34 55
while loop
The while loop is another kind of loop iterated until a condition is satisfied. The testing expression is checked first before executing the body of the loop. Syntax :
while (condition)
body
end
(endwhile can also be used)
Example : Display numbers from 1 to 10 :
MATLAB
i = 1;
while i <= 10
disp(i);
i = i + 1;
endwhile
|
Output :
1
2
3
4
5
6
7
8
9
10
break statement
It is used to exit from a loop. Example 1 : We will be making a row vector and will only modify the first 6 values using the break statement.
MATLAB
v = [1:10:100];
for i = 1:10,
v(i) = 0;
if i == 6,
break ;
end ;
end ;
disp(v)
|
Output :
0 0 0 0 0 0 61 71 81 91
Example 2 : break statement with while loop :
MATLAB
i = 1;
while true
disp(i);
disp(" is less than 5");
i = i + 1;
if i == 5,
break ;
end ;
end ;
|
Output :
1
is less than 5
2
is less than 5
3
is less than 5
4
is less than 5
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...