How to terminate a loop

N

noreply

Guest
Hi all ,
What is the way to terminate a loop?.
Say in my case,
I'm running a "foreach loop" and i want to break the loop after a
certain number of iterations. How to go about it?

regards,
Lokesh
 
noreply wrote, on 07/28/09 09:51:
Hi all ,
What is the way to terminate a loop?.
Say in my case,
I'm running a "foreach loop" and i want to break the loop after a
certain number of iterations. How to go about it?

regards,
Lokesh
Well, the simplest is to use forall() or exists(), which continue traversing a
loop whilst a condition holds non-nil (for forall), or becomes non-nil (for exists).

Otherwise you can use prog() with return(). Personally I don't like this
approach (but then I don't like using break inside a for loop in C either; it's
a step along the road to "spaghetti programming").

; use of prog/return to jump out of the loop. List of
; local vars is empty in the prog, because it's only being used
; as a means to exit early.
l='(1 2 3 4 5)
prog(()
foreach(elem l
when(elem==4 return(t))
printf("Elem is %d\n" elem)
)
)

; alternative using forall and no pasta
forall(elem l
if(elem==4 then nil
else printf("Elem is %d\n" elem)
)
)

Regards,

Andrew.
 

Welcome to EDABoard.com

Sponsor

Back
Top