This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
 Documentation / Mathematica / Built-in Functions / Programming / Flow Control  /
While

  • While[ test , body ] evaluates test, then body, repetitively, until test first fails to give True.
  • While[ test ] does the loop with a null body.
  • If Break[ ] is generated in the evaluation of body, the While loop exits.
  • Continue[ ] exits the evaluation of body, and continues the loop.
  • Unless Return[ ] or Throw[ ] are generated, the final value returned by While is Null.
  • Example: i=0; While[i < 0, tot += f[i]; i++]. Note that the roles of ; and , are reversed relative to the C programming language.
  • See the Mathematica book: Section 2.5.9.
  • See also: Do, For, Nest, Fold, Select, Throw.

    Further Examples

    The While loop continues until the condition fails.

    In[1]:=

    9

    8

    7

    6

    In[2]:=