Steven P Posted May 5, 2017 Posted May 5, 2017 Good morning, Is there something similar to a while loop that will exit the loop part way through if the condition is reached? For example (setq test 0) (while (<= test 10) (setq test (1+ test)) (princ test) (setq test (1+ test)) (princ test) (setq test (1+ test)) (princ test) ) Will return a count in the command line, 1 to 12, then the next time round 'while' sees 'test' is greater than 10 and doesn't do the loop again. What I am wanting is the loop to count 1 to 10, then exit the while loop. So is there anything simple that will do this? Thanks Quote
Tharwat Posted May 5, 2017 Posted May 5, 2017 Hi, As long as you have specific number of loops then you can use REPEAT function but while function also should work. (repeat 10 (......) ) Quote
Grrr Posted May 5, 2017 Posted May 5, 2017 (while <test statement> [color="green"]; if true, do the expressions below, if false - don't evaluate the expressions/stop the loop[/color] <expressions> [color="green"]; evaluate the expressions, and run the <test statement> again[/color] ) Whats happening is that you are incrementing the value 3 times per check, I think that you have to increase the value once at every check: (setq n 0) (while (< n 10) (setq n (1+ n)) (print n) ) Or check the every sub-increment, which is frustrating: (setq n 0) (while (<= n 10) (while (<= (setq n (1+ n)) 10) (print n) (while (<= (setq n (1+ n)) 10) (print n) (while (<= (setq n (1+ n)) 10) (print n) ) ) ) ) But if you really just want to increment like that, use repeat function like Tharwat mentioned (its the fastest). Quote
Steven P Posted May 5, 2017 Author Posted May 5, 2017 Thanks, perhaps I should have explained it better. Replace (setq test (1+ test)) with calls to other subroutines, each of these might take Test to above 10 sometime, but I don't want the subsequent sub routines to run when it does (for example 1st loop, routine_1 and test = 3, routine_2, test = 5, routine_3 test = 8, 2nd loop, routine 1 and test = 9 routine_2 test = 12 so stop loop before routine_3 is called) Tharwat - repeat works well for how I first described my problem, thanks Grrr - I want to do the frustrating method but in a less frustrating manner Quote
Lee Mac Posted May 5, 2017 Posted May 5, 2017 Maybe something like this? (setq test 0) (while (and (< test 11) (progn (setq test (1+ test)) (< test 11) ) (progn (setq test (1+ test)) (< test 11) ) (progn (setq test (1+ test)) (< test 11) ) ) ) Quote
Steven P Posted May 8, 2017 Author Posted May 8, 2017 Thanks Lee, as always a great solution, I never knew you could add 'and' to a while command like that Quote
Lee Mac Posted May 8, 2017 Posted May 8, 2017 Thanks Lee, as always a great solution, I never knew you could add 'and' to a while command like that You're welcome Steven - any expression can constitute the test expression for a while loop. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.