Loops (For and While) and Control Statements in Octave

01 Aug, 2020
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.
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 :
% initializing variables variable1 and variable2 variable1 = 20;variable2 = 20;   % check the if conditionif variable1 == variable2,   % if the condition is true this statement will executedisp('The variables are Equal');   % end the if statementendif;
Output:
The variables are Equal

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

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

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 :
% the value of i will move from 1 to 5% with an increment of 1for i = 1:5,  % displays value of idisp(i);  % end the for loopend;
Output :
 1
 2
 3
 4
 5

Example 2 : for loop with vectors :
% making a column vector from 1 to 10v = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10];  % the value of i will move from 1 to 10 % with an increment of 1for 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 loopend;    % displays the v vector with modified valuesdisp(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 :
% 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 1for 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 loopendfor  % print the fibonacci seriesdisp(fibonacci)
Output :
    1    1    2    3    5    8   13   21   34   55

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 :
% initializing the variable i with 1i = 1;  % while conditionwhile i <= 10  % displaying the value of idisp(i);  % make an increment of 1 in the value of ii = i + 1;  % end the while loopendwhile
Output :
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10

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.
% making a row vector of 1x10, starting from 1 % and the next value is +10 of it's previous valuev = [1:10:100];  % the value of i will move from 1 to 10 % with an increment of 1for i = 1:10,  % making the ith element in vector to 0v(i) = 0;  % if the condition is true the break statement % will execute and the loop will terminateif i == 6,break;  % end the if conditionend;  % end the for loopend;  % displays the modified vector vdisp(v)
Output :
    0    0    0    0    0    0   61   71   81   91

Example 2 : break statement with while loop :
% initializing the variable i with 1i = 1;  % the while condition is always truewhile true  % display the value of idisp(i);  % display the below contentdisp(" is less then 5");  % make an increment of 1 in value of ii = i + 1;  % if the if condition is true loop will break if i == 5,break;  % end the if statementend;  % end the whileend;
Output :
 1
 is less then 5
 2
 is less then 5
 3
 is less then 5
 4
 is less then 5

Octave-GNU
Programming Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Decorators with parameters in Python
C# | Data Types
Shallow Copy and Deep Copy in C++
Difference between Shallow and Deep copy of a class
Kotlin Array
Top 10 Programming Languages to Learn in 2022
Advantages and Disadvantages of OOP
Top 10 Fastest Programming Languages
Difference between while and do-while loop in C, C++, Java
Introduction of Object Oriented Programming