Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #7124

Re: While Loop

From "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: While Loop
Date 2011-12-11 08:59 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jc1rdh$t4g$1@smc.vnet.net> (permalink)
References <jbvjus$j47$1@smc.vnet.net>

Show all headers | View raw


On Sat, 10 Dec 2011 12:39:56 -0000, Joao <joaopereira9@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.

Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

While Loop Joao <joaopereira9@gmail.com> - 2011-12-10 12:39 +0000
  Re: While Loop "Nasser M. Abbasi" <nma@12000.org> - 2011-12-11 08:56 +0000
  Re: While Loop "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-12-11 08:58 +0000
  Re: While Loop "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-12-11 08:59 +0000
  Re: While Loop DrMajorBob <btreat1@austin.rr.com> - 2011-12-12 11:45 +0000
  Re: While Loop David Bailey <dave@removedbailey.co.uk> - 2011-12-13 10:45 +0000
  Re: While Loop João Paulo Pereira <joaopereira9@gmail.com> - 2011-12-13 10:56 +0000

csiph-web