How to terminate a loop

Discussion in 'Cadence' started by noreply, Jul 28, 2009.

  1. noreply

    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, Jul 28, 2009
    #1
  2. noreply wrote, on 07/28/09 09:51:
    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.
     
    Andrew Beckett, Jul 28, 2009
    #2
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.