Print
MVEL 2.0 Control Flow

MVEL's power goes beyond simple expressions. In fact, MVEL supports an assortment of control flow operators which will allow you to perform advanced scripting operations.

If-Then-Else

MVEL supports full, C/Java-style if-then-else blocks. For example:

Ternary statements

Ternary statements are supported just as in Java:

And nested ternary statements:

Foreach

One of the most powerful features in MVEL is it's foreach operator. It is similar to the for each operator in Java 1.5 in both syntax and functionality. It accepts two parameters separated by a colon, the first is the local variable for the current element, and the second is the collection or array to be iterated.

For example:

Since MVEL treats Strings as iterable objects you can iterate a String (character by character) with a foreach block:

The above example outputs: [A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][V][W][X][Y][Z]

You can also use MVEL to count up to an integer value (from 1):

Which outputs: 123456789

Syntax Note: As of MVEL 2.0, it is now possible to simply abbreviate foreach in the same way it is in Java 5.0, by using the for keyword. For example:

For Loop

MVEL 2.0 implements standard C for-loops:

Do While, Do Until

do while and do until are implemented in MVEL, following the same convention as Java, with until being the inverse of while.

... is semantically equivalent to ...

While, Until

MVEL 2.0 implements standard while, with the addition of the inverse until.

... or ...

Powered by Atlassian Confluence