Mathematica 9 is now available
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.
Mathematica > Core Language > Procedural Programming > Looping Constructs >

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.
  • 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.
Print and increment n while the condition n<4 is satisfied:
Print and increment n while the condition n<4 is satisfied:
The body can be included as part of the test:
Break breaks out of the While:
Test the condition during or at the end of the loop:
Compute the GCD of two numbers:
Count the number of iterations in a fixed-point calculation:
If the test is False, the body is never evaluated:
While returns Null:
Use Print to see the iterations:
Do iterates a fixed number of times:
NestWhile is a functional form of While:
New in 1
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team