MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: While Loop

  • To: mathgroup at smc.vnet.net
  • Subject: [mg123571] Re: While Loop
  • From: "Oleksandr Rasputinov" <oleksandr_rasputinov at hmamail.com>
  • Date: Sun, 11 Dec 2011 03:49:23 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <jbvjus$j47$1@smc.vnet.net> <op.v596hug2qcgwdu@core2.lan>

On Sat, 10 Dec 2011 15:54:56 -0000, 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.

Sorry, I should have said "left to right" in the above but was apparently  
not paying enough attention to what I was writing. Functions that violate  
this convention often call it out specifically in their documentation; For  
is an example of a function with non-standard evaluation order.



  • Prev by Date: Re: Crashing problem with 3d IFS program
  • Next by Date: Re: Can FindFit take two (or more) equations?
  • Previous by thread: Re: While Loop
  • Next by thread: Re: While Loop