Re: While Loop
- To: mathgroup at smc.vnet.net
- Subject: [mg123573] Re: While Loop
- From: "Oleksandr Rasputinov" <oleksandr_rasputinov at hmamail.com>
- Date: Sun, 11 Dec 2011 03:49:44 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <jbvjus$j47$1@smc.vnet.net>
On Sat, 10 Dec 2011 12:39:56 -0000, Joao <joaopereira9 at gmail.com> wrote: > Hi, everybody > > Could someone help me regarding the While Loop. > > While[test,body] > > In the help "While" it is stated that the test may be evaluated at the > end of the loop, instead of at the beginning. However in the tutorial on > Loops and Control structures it is stated that the While always > evaluates the test at the beginning. > > I would like to know if it is really possible to evaluate the test > inside de While at the end of the loop. For the syntactical form you give above, test (not "the test", but "the expression called test") is always evaluated before body (ditto). This is standard in Mathematica in that function arguments normally evaluate from right to left. However, what the documentation is saying is that there are variant forms you can use to have "the test" evaluated after "the body". You can, of course, use While[True, body; If[test, Break[]]] but I think the clearest way is the one which omits body entirely ("the expression called body" is implicitly Null). For example, i = 5; While[Print[--i]; i > 0] which prints 4 3 2 1 0 and thus demonstrates that it meets your requirements.
- Follow-Ups:
- Re: While Loop
- From: DrMajorBob <btreat1@austin.rr.com>
- Re: While Loop