Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Alternative to While?

3 REPLIES 3
Reply
Message 1 of 4
Anonymous
150 Views, 3 Replies

Alternative to While?

I've been using While for a long time but never really understood exactly
when the exit from the expression took place. The following snippet proved
to me when that happens and it is a the bottom of the While, not when the
exit condition takes place. In the following, Var Second gets incremented
to 1 and the alert box displays yet Abort has already been set to T.

Is there some technique I can use (other than Quit or Exit) to get out from
the middle?

(setq Abort nil
First 0
Second 0
)
(while (equal Abort nil)
(setq First (1+ First))
(setq Abort T)
(setq Second (1+ Second))
(alert "This is the bottom")
)

Len Miller
--
To email reply, eradicate all threes in my SPAM guarded address.
3 REPLIES 3
Message 2 of 4
Anonymous
in reply to: Anonymous

The while command checks the condition (equal Abort nil) and, if it's true,
runs the rest of the statements, then loops back to check the condition
again. If the condition is false none of the rest of statements will be
processed.

Try something like:

(defun C:Test ( / Abort First Second)
(setq Abort nil
First 0
Second 0
)
(while (equal Abort nil)
(setq First (1+ First))
(initget "Yes No")
(setq abort (equal (getkword " Continue?: /N: ") "No"))
(if (not abort)
(progn
(setq Second (1+ Second))
(alert "This is the bottom")
)
)

)
(princ)
)
Message 3 of 4
Anonymous
in reply to: Anonymous

Sure. Just put some conditions on whether or not Abort gets set to T.
...
(if [some event occurs]
(set Abort T)
)
...

If you want it to skip out of the routine immediately when Abort goes True,
just enclose the remaining part of the loop in an (if ...) clause:

(if (not Abort)
(setq Second (1+ Second))
)
(alert "This is the bottom")
...

What [some event] might be depends on what processes the loop might be
performing.
___

"Fatfreek" wrote in message
news:4908181@discussion.autodesk.com...

Is there some technique I can use (other than Quit or Exit) to get out from
the middle?

(setq Abort nil
First 0
Second 0
)
(while (equal Abort nil)
(setq First (1+ First))
(setq Abort T)
(setq Second (1+ Second))
(alert "This is the bottom")
)

Len Miller
--
To email reply, eradicate all threes in my SPAM guarded address.
Message 4 of 4
Anonymous
in reply to: Anonymous

"The following snippet proved to me when that happens and it is a the bottom of the While, not when the exit condition takes place."

Nope. The actual exit happens at the top, where the test expression is. It doesn't matter when the "exit condition" occurs, it matters when you test for it, and in a while that's at the top of the loop. The first time your loop returns to the test, and abort has been set to T, then the while loop is exited.

Sometimes I set them up like this:

(while (not done)
(do-some-stuff)
(if (whatever) (setq done t))
)

Done is a local var and therefore initially nil.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


Autodesk Design & Make Report