({{, (RefLink[For, paclet:ref/For])[start,test,incr,body]
executes start, then repeatedly evaluates body and incr until test fails to give (RefLink[True, paclet:ref/True]). }})

OpenersNotesSection(Spacer1)Details

(RefLink[For, paclet:ref/For]) evaluates its arguments in a nonstandard way.

(RefLink[For, paclet:ref/For])[start,test,incr] does the loop with a null body.

The sequence of evaluation is test, body, incr. (RefLink[For, paclet:ref/For]) exits as soon as test fails.

If (RefLink[Break, paclet:ref/Break])[] is generated in the evaluation of body, the (RefLink[For, paclet:ref/For]) loop exits.

(RefLink[Continue, paclet:ref/Continue])[] exits the evaluation of body, and continues the loop by evaluating incr.

Unless an explicit (RefLink[Return, paclet:ref/Return]) is used, the value returned by (RefLink[For, paclet:ref/For]) is (RefLink[Null, paclet:ref/Null]).

Note that in an example like (RefLink[For, paclet:ref/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.

OpenersPrimaryExamplesSection(Spacer1)Examples  (5)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

For[i=0,i<4,i++,Print[i]]

A comma delimits the parts of (RefLink[For, paclet:ref/For]); a semicolon delimits the parts of procedures:

For[i=1;t=x,i^2<10,i++,t=t^2+i;Print[t]]

OpenersExampleSection1(Spacer1)Scope  (2)

(RefLink[Break, paclet:ref/Break]) breaks out of (RefLink[For, paclet:ref/For]):

For[i=1,i<1000,i++,If[i>10,Break[]]];i

(RefLink[Continue, paclet:ref/Continue]) continues the loop without executing the rest of the body:

For[t=1;k=1,k≤5,k++,t*=k;Print[t];If[k<2,Continue[]];t+=2]

OpenersExampleSection2(Spacer1)Properties & Relations  (1)

A (RefLink[Do, paclet:ref/Do]) loop can be written as a (RefLink[For, paclet:ref/For]) loop:

Do[Print[i],{i,1,3}]

For[i=1,i≤3,i++,Print[i]]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Do, paclet:ref/Do])  ▪  (RefLink[While, paclet:ref/While])  ▪  (RefLink[Throw, paclet:ref/Throw])  ▪  (RefLink[NestWhile, paclet:ref/NestWhile])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Loops and Control Structures, paclet:tutorial/LoopsAndControlStructures])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Looping Constructs, paclet:guide/LoopingConstructs])}, {▪(RefLinkPlain[Procedural Programming, paclet:guide/ProceduralProgramming])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(WebLink[DocumentationFooterIconsRelatedLinks-FastIntro.png(Spacer1)Fast Introduction for Programmers: Iterators, http://www.wolfram.com/language/fast-introduction-for-programmers/iterators/])}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/38-assigning-names-to-things.html])(WebLink[: Assigning Names to Things, https://www.wolfram.com/language/elementary-introduction/38-assigning-names-to-things.html])}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/46-writing-good-code.html])(WebLink[: Writing Good Code, https://www.wolfram.com/language/elementary-introduction/46-writing-good-code.html])}, {(WebLink[DocumentationFooterIconsRelatedLinks-NKS.png(Spacer1)NKS|Online, http://www.wolframscience.com/nksonline/index/search.cgi?SearchIndex=For]) ((WebLink[A New Kind of Science, http://www.wolframscience.com/nksonline/]))}})}})