Control Flow

Decisions

Branching

Looping


if

if else

if else if else

switch

break

Use the break statement to exit a loop and resume the execution of the parent block. Break can be combined with a label to go to a specific loop.

continue

Use continue to skip an iteration of a loop and resume at the next iteration

return

Functions in Lucee can have multiple return values which can exit a function early for controlling flow.

for loop

for in loop

The for in construct can be used with for arrays, structs, and queries. Below is an example for arrays. For other examples, see:

Do While loop

While Loop

Loop Tag

The loop tag is an alternative style which has additional features for arrays, structs, queries and lists

See:

Looping with Labels

It is possible to break; out of loop and continue; loops and resume execution at particular lables. This is particularly useful when executing nested loops, and when breaking out of a child loop, wanting to continue execution at a parent, or after a parent.

For Loop with Label;

Nested For Loops with Labels

This exits out of the child loop and continues after the parent loop

Nested Loop tags with Label

Labels are also possible with loop tags using the label attribute

Nested Loop using Continue

While the previous examples used break; to exit the loop and resume after the label, its also possible to use continue, which results the next iteration of the loop, at the label:

The above sample would produce:

inner loop with 1
inner loop with 2
Top of loop with 2
inner loop with 1
inner loop with 2
Top of loop with 3
inner loop with 1
inner loop with 2
Top of loop with 4
inner loop with 1
inner loop with 2
Top of loop with 5
inner loop with 1
inner loop with 2
After loop

The sample example but with a break

, produces:

Top of loop with 1
inner loop with 1
inner loop with 2
After loop

results matching ""

    No results matching ""