This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


while loop to repeat when condition is true


while expression



while expression, statements, end evaluates an expression, and repeats the execution of a group of statements in a loop while the expression is true. An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric). Otherwise, the expression is false.


collapse all

Use a while loop to calculate factorial(10).

n = 10;
f = n;
while n > 1
    n = n-1;
    f = f*n;
disp(['n! = ' num2str(f)])
n! = 3628800

Count the number of lines of code in the file magic.m. Skip blank lines and comments using a continue statement. continue skips the remaining instructions in the while loop and begins the next iteration.

fid = fopen('magic.m','r');
count = 0;
while ~feof(fid)
    line = fgetl(fid);
    if isempty(line) || strncmp(line,'%',1) || ~ischar(line)
    count = count + 1;
count = 31

Sum a sequence of random numbers until the next random number is greater than an upper limit. Then, exit the loop using a break statement.

limit = 0.8;
s = 0;

while 1
    tmp = rand;
    if tmp > limit
    s = s + tmp;

More About

collapse all


An expression can include relational operators (such as < or ==) and logical operators (such as &&, ||, or ~). Use the logical operators and and or to create compound expressions. MATLAB® evaluates compound expressions from left to right, adhering to operator precedence rules.

Within the conditional expression of a while...end block, logical operators & and | behave as short-circuit operators. This behavior is the same as && and ||, respectively. Since && and || consistently short-circuit in conditional expressions and statements, it is good practice to use && and || instead of & and | within the expression. For example,

x = 42;
while exist('myfunction.m','file') && (myfunction(x) >= pi)
    disp('Expressions are true')

The first part of the expression evaluates to false. Therefore, MATLAB does not need to evaluate the second part of the expression, which would result in an undefined function error.


  • If you inadvertently create an infinite loop (that is, a loop that never ends on its own), stop execution of the loop by pressing Ctrl+C.

  • If the conditional expression evaluates to a matrix, MATLAB evaluates the statements only if all elements in the matrix are true (nonzero). To execute statements if any element is true, wrap the expression in the any function.

  • To programmatically exit the loop, use a break statement. To skip the rest of the instructions in the loop and begin the next iteration, use a continue statement.

  • When nesting a number of while statements, each while statement requires an end keyword.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Introduced before R2006a

Was this topic helpful?