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


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

Re: While Loop

From "Nasser M. Abbasi" <nma@12000.org>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: While Loop
Date 2011-12-11 08:56 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jc1r7o$t38$1@smc.vnet.net> (permalink)
References <jbvjus$j47$1@smc.vnet.net>

Show all headers | View raw


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





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