For

For[start,test,incr,body]

executes start, then repeatedly evaluates body and incr until test fails to give True.

Details

  • For evaluates its arguments in a nonstandard way.
  • For[start,test,incr] does the loop with a null body.
  • The sequence of evaluation is test, body, incr. For exits as soon as test fails.
  • If Break[] is generated in the evaluation of body, the For loop exits.
  • Continue[] exits the evaluation of body, and continues the loop by evaluating incr.
  • Unless an explicit Return is used, the value returned by For is Null.
  • Note that in an example like For[tot=0;i=0,i<3,i++,tot+=f[i]] the roles of the semicolon and comma are reversed relative to C-like programming languages.

Examples

open allclose all

Basic Examples  (2)

A comma delimits the parts of For; a semicolon delimits the parts of procedures:

Scope  (2)

Break breaks out of For:

Continue continues the loop without executing the rest of the body:

Properties & Relations  (1)

A Do loop can be written as a For loop:

Wolfram Research (1988), For, Wolfram Language function, https://reference.wolfram.com/language/ref/For.html.

Text

Wolfram Research (1988), For, Wolfram Language function, https://reference.wolfram.com/language/ref/For.html.

CMS

Wolfram Language. 1988. "For." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/For.html.

APA

Wolfram Language. (1988). For. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/For.html

BibTeX

@misc{reference.wolfram_2024_for, author="Wolfram Research", title="{For}", year="1988", howpublished="\url{https://reference.wolfram.com/language/ref/For.html}", note=[Accessed: 19-March-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_for, organization={Wolfram Research}, title={For}, year={1988}, url={https://reference.wolfram.com/language/ref/For.html}, note=[Accessed: 19-March-2024 ]}