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


Groups > comp.soft-sys.math.mathematica > #7093 > unrolled thread

While Loop

Started byJoao <joaopereira9@gmail.com>
First post2011-12-10 12:39 +0000
Last post2011-12-13 10:56 +0000
Articles 7 — 6 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  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

#7093 — While Loop

FromJoao <joaopereira9@gmail.com>
Date2011-12-10 12:39 +0000
SubjectWhile Loop
Message-ID<jbvjus$j47$1@smc.vnet.net>
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. This is a feature that would be useful for what I'm doing, however I haven't been able to get it working and I don't know, taking into consideration the above, if I am doing something wrong or if it is really not possible to do.

Thanks in advance

Joao

[toc] | [next] | [standalone]


#7118

From"Nasser M. Abbasi" <nma@12000.org>
Date2011-12-11 08:56 +0000
Message-ID<jc1r7o$t38$1@smc.vnet.net>
In reply to#7093
On 12/10/2011 6:39 AM, Joao 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,

The test is always done at the top.

ButThe example you refer to, which is

------------------------------------
Test the condition during or at the end of the loop:

While[True,
  n = Input["enter an integer"];
  If[! IntegerQ[n] || n <= 0, Break[]];
  Print[n, "=", FactorInteger[n]]
  ]
----------------------------

The 'test' which is being done at the end of the loop above is not the same
test which 'While' does automatically.

It is meant to show that one can manually do a check at the end (or at any
point for that matter), and then use Break to manually end the loop. May
be the example can be re-written better that it was.

Some languages even has a construct like this

LOOP
  ..
  ..
  IF(...) THEN Break;
END LOOP

Not a 'while' and not a 'Do', but just a LOOP where one must use Break
to exists the loop manually.

>  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. This is a feature that would be useful for what I'm doing,

Yes you can. See the example above. You need to do it manually and use Break to exit
the loop.

>however I haven't been able to get it working and I don't know, taking into consideration
>the above, if I am doing something wrong or if it is really not possible to do.
>
> Thanks in advance
>
> Joao
>

If that is what you want, (i.e run the loop at least ONE time, and
then do a check at the end) then may be you can use Do[].  Like this:

---------------------
Clear[i]
Do[
  Print["i=", i];

  If[i == 3, Break[]],  (*the check is done at the bottom of the loop*)

  {i, 0, 10}
]
--------------------

In the above, the loop will at least run ONE time, as compared to
the While loop, which is possible to run zero times since the check
is done at the start of the loop.

-------------- runs at least ONE time -------------------
Clear[i]
Do[
  Print["i=", i];
  If[i == 0, Break[]],  (*the check is done at the bottom of the loop*)
  {i, 0, 10}
  ]
--------------------------------------------------------

The above will print '0', then stops.

--Nasser





[toc] | [prev] | [next] | [standalone]


#7122

From"Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com>
Date2011-12-11 08:58 +0000
Message-ID<jc1rbk$t44$1@smc.vnet.net>
In reply to#7093
On Sat, 10 Dec 2011 15:54:56 -0000, Oleksandr Rasputinov  
<oleksandr_rasputinov@hmamail.com> wrote:

> 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.

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.

[toc] | [prev] | [next] | [standalone]


#7124

From"Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com>
Date2011-12-11 08:59 +0000
Message-ID<jc1rdh$t4g$1@smc.vnet.net>
In reply to#7093
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.

[toc] | [prev] | [next] | [standalone]


#7141

FromDrMajorBob <btreat1@austin.rr.com>
Date2011-12-12 11:45 +0000
Message-ID<jc4pgh$cjp$1@smc.vnet.net>
In reply to#7093
Nice.

Bobby

On Sun, 11 Dec 2011 02:49:44 -0600, Oleksandr Rasputinov  
<oleksandr_rasputinov@hmamail.com> wrote:

> 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.
>


-- 
DrMajorBob@yahoo.com

[toc] | [prev] | [next] | [standalone]


#7168

FromDavid Bailey <dave@removedbailey.co.uk>
Date2011-12-13 10:45 +0000
Message-ID<jc7ad4$ose$1@smc.vnet.net>
In reply to#7093
On 10/12/2011 12:39, Joao 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. This is a feature that would be useful for what I'm doing, however I haven't been able to get it working and I don't know, taking into consideration the above, if I am doing something wrong or if it is really not possible to do.
>
> Thanks in advance
>
> Joao
>
I think you are perhaps referring to this example from the help:

While[True,
  n = Input["enter an integer"];
  If[! IntegerQ[n] || n <= 0, Break[]];
  Print[n, "=", FactorInteger[n]]
  ]

In this case, the "test" is just True - it will always pass - but the 
"real test" is the line:

If[! IntegerQ[n] || n <= 0, Break[]];

This breaks out of the loop if n is not an integer greater than 0.

Thus, in reality, a loop can be structured with exit(s) wherever you like.

BTW, Unlike some Mathematica programmers, I think the While construct is 
extremely useful for handling some looping situations, but it is always 
worthwhile considering if a functional programming construct, such as 
Map, would achieve the same result.

David Bailey
http://www.dbaileyconsultancy.co.uk


[toc] | [prev] | [next] | [standalone]


#7189

FromJoão Paulo Pereira <joaopereira9@gmail.com>
Date2011-12-13 10:56 +0000
Message-ID<jc7b0f$p2q$1@smc.vnet.net>
In reply to#7093
Thanks everybody

It is clear now how this works

Joao




On 12/12/11, DrMajorBob <btreat1@austin.rr.com> wrote:
> Nice.
>
> Bobby
>
> On Sun, 11 Dec 2011 02:49:44 -0600, Oleksandr Rasputinov
> <oleksandr_rasputinov@hmamail.com> wrote:
>
>> 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.
>>
>
>
> --
> DrMajorBob@yahoo.com
>
>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web