Re: While Loop
- To: mathgroup at smc.vnet.net
- Subject: [mg123584] Re: While Loop
- From: DrMajorBob <btreat1 at austin.rr.com>
- Date: Mon, 12 Dec 2011 06:42:55 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <jbvjus$j47$1@smc.vnet.net> <201112110849.DAA29692@smc.vnet.net>
- Reply-to: drmajorbob at yahoo.com
Nice. Bobby On Sun, 11 Dec 2011 02:49:44 -0600, Oleksandr Rasputinov <oleksandr_rasputinov at hmamail.com> wrote: > 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. > -- DrMajorBob at yahoo.com
- References:
- Re: While Loop
- From: "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com>
- Re: While Loop