Open In App

Loops (For and While) and Control Statements in Octave

Last Updated : 18 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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




% initializing variables variable1 and variable2
variable1 = 20;
variable2 = 20;
  
% check the if condition
if variable1 == variable2,
  
% if the condition is true this statement will execute
disp('The variables are Equal');
 
% end the if statement
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




% initializing variables variable1 and variable2
variable1 = 20;
variable2 = 40;
 
% check the if condition
if variable1 == variable2,
 
% if the condition is true this statement will execute
disp('The variables are Equal');
 
% if the condition is false else statement will execute
else
disp('The variables are Not Equal');
  
%end the if-else statement
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




% initializing the variable var
var = 50;
 
% check the if condition
if var < 50,
disp('The variable is less than 50');
 
% check the elseif condition
elseif var > 50,
disp('The variable is greater than 50');
 
% if both the above condition is false else
% statement will execute
else
disp('The variable is 50');
 
% end the if..elseif.. statements
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




% the value of i will move from 1 to 5
% with an increment of 1
for i = 1:5,
 
% displays value of i
disp(i);
 
% end the for loop
end;


Output :

 1
 2
 3
 4
 5

Example 2 : for loop with vectors : 

MATLAB




% making a column vector from 1 to 10
v = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10];
 
% the value of i will move from 1 to 10
% with an increment of 1
for i = 1:10,
  
% modifying the value of ith element
% in the column vector as v(i) * 10
v(i) = v(i) * 10;
  
% end the for loop
end;
  
% displays the v vector with modified values
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




% create a row vector of 10 elements all as '1'
fibonacci = ones(1, 10);
 
% the value of i will move from 3 to 10 over the
% increment of 1
for i = 3:10
 
% the ith term of fibonacci will computed
% as the sum of its previous 2 terms
    fibonacci(i) = fibonacci(i - 1) + fibonacci(i - 2);
 
% end the for loop
endfor
 
% print the fibonacci series
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




% initializing the variable i with 1
i = 1;
 
% while condition
while i <= 10
 
% displaying the value of i
disp(i);
 
% make an increment of 1 in the value of i
i = i + 1;
 
% end the while loop
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




% making a row vector of 1x10, starting from 1
% and the next value is +10 of it's previous value
v = [1:10:100];
 
% the value of i will move from 1 to 10
% with an increment of 1
for i = 1:10,
 
% making the ith element in vector to 0
v(i) = 0;
 
% if the condition is true the break statement
% will execute and the loop will terminate
if i == 6,
break;
 
% end the if condition
end;
 
% end the for loop
end;
 
% displays the modified vector v
disp(v)


Output :

    0    0    0    0    0    0   61   71   81   91

Example 2 : break statement with while loop : 

MATLAB




% initializing the variable i with 1
i = 1;
 
% the while condition is always true
while true
 
% display the value of i
disp(i);
 
% display the below content
disp(" is less than 5");
 
% make an increment of 1 in value of i
i = i + 1;
 
% if the if condition is true loop will break
if i == 5,
break;
 
% end the if statement
end;
 
% end the while
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
Previous
Next
Share your thoughts in the comments

Similar Reads