English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

MATLAB While Loop

Matlab Loop Statements

The while loop repeats the statements as long as the condition is true.

Syntax

The syntax of the while loop in MATLAB is-

while <expression>
   <statements>
end

The while loop repeats the program statements as long as the expression (statements) remains true.

The expression is true when the result is non-empty and contains all non-zero elements (logical or real values). Otherwise, the expression is false.

Online Example

Create a script file and enter the following code-

a = 10;
% while loop execution 
while( a < 20 )
   fprintf('value of a: %d\n', a);
   a = a + 1;
end
It displays the following result when the file is executed-
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
value of a: 18
value of a: 19

Matlab Loop Statements