While

While[test,body]

evaluates test, then body, repetitively, until test first fails to give True.

Details

  • 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 an explicit Return is used, the value returned by While is Null.
  • Note that in an example like i=0;While[i<0,tot+=f[i];i++] the roles of ; and , are reversed relative to C-like programming languages.

Examples

open allclose all

Basic Examples  (1)

Print and increment n while the condition n<4 is satisfied:

In[1]:=
Click for copyable input

Scope  (2)

Generalizations & Extensions  (1)

Applications  (2)

Properties & Relations  (4)

See Also

Do  For  NestWhile  TakeWhile  LengthWhile  Nest  Fold  Select  Throw

Tutorials

Introduced in 1988
(1.0)