Loops (For and While) and Control Statements in Octave

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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

% 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;

chevron_right


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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

% 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

chevron_right


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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

% 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;

chevron_right


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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

% 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;

chevron_right


Output :



 1
 2
 3
 4
 5

Example 2 : for loop with vectors :

filter_none

edit
close

play_arrow

link
brightness_4
code

% 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)

chevron_right


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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

% 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)

chevron_right


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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

% 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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

% 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)

chevron_right


Output :

    0    0    0    0    0    0   61   71   81   91

Example 2 : break statement with while loop :

filter_none

edit
close

play_arrow

link
brightness_4
code

% 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 then 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;

chevron_right


Output :

 1
 is less then 5
 2
 is less then 5
 3
 is less then 5
 4
 is less then 5



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.