MATLAB - The While Loop
MATLAB - The While Loop
Syntax
The syntax of a while loop in MATLAB is −
while <expression>
<statements>
end
The while loop repeatedly executes program statement(s) as long as the expression remains true.
An expression is true when the result is nonempty and contains all nonzero elements (logical or real
numeric). Otherwise, the expression is false.
Example
Create a script file and type the following code −
a = 10;
Live Demo
% while loop execution
while( a < 20 )
fprintf('value of a: %d\n', a);
a = a + 1;
end
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
Page 2 of 2
value of a: 18
value of a: 19