Search code examples
mvel

how to break or continue in MVEL for / foreach loop


I see MVEL supports for loop and foreach templating, but how to "break" or "continue" from the loop?


Solution

  • No mention of support of 'break' or 'continue' in the documentation: http://mvel.codehaus.org/MVEL+2.0+Control+Flow.

    The closest I could find is a user group email in 2009, stating that there's NO support of break or continue: http://markmail.org/message/rgyqvwhiedfpcchj

    you could still achieve the same effect as "break" this way (not the cleanest code in the world):

    skip_rest = false;
    for(item: collection) {
       if (!skip_rest) {
         /* do something */
         if (some condition) {
           /* break by skipping */
           skip_rest = true; 
         }
       }
    }
    

    You got the idea, similar thing can be done by flag setting to achieve 'continue'.