Cicada ---> Online Help Docs ---> Cicada scripting ---> Loops and if blocks

break

There is no break statement in Cicada. But there is a way to jerry-rig something that does pretty much the same. The trick is to put braces -- with no code marker or semicolon -- around the code we eventually want to escape from. Within those braces, a return statement will simply escape from the bracketed code and continue with what follows. For example:


    {
       for counter in <1, 10> (
           print(counter)
           if input() == "q" then return   )
    }
    print("finished with counter = ", counter)
   

This works because the braces define a new composite variable, and the code inside the braces is actually running inside of this variable. This hack causes problems in some cases. If we define a member, that member will be inside of this variable, which was probably not intended. The this keyword refers to this newly-defined variable, while parent now refers to the place we were before hitting the braces. Also, if we had been running a function, we cannot escape this function using a return statement within the bracketed code. On the upside, we have a lot of latitude in choosing where to put the braces: it doesn’t have to escape from a loop, and if we do use them inside loops it doesn’t have to fall out to the next-outermost for or while.


Prev: backfor    Next: Sets


Last update: May 8, 2024