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

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.

Similar Reads

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-else condition

...

if-elseif condition

It is similar to if condition but when the test expression in if condition fails, then statements in else condition are executed. Syntax :...

for loop

...

while loop

When the first if condition fails, we can use elseif to supply another if condition. Syntax :...

break statement

...

Contact Us