Skip to content

Latest commit

 

History

History
133 lines (90 loc) · 4.95 KB

control_flow.md

File metadata and controls

133 lines (90 loc) · 4.95 KB

Control Flow

##Decisions

##Branching

##Looping


###if

{% gist id="https://gist.github.com/roryl/11c5a5ab8cf061ab1621",file="if.cfm" %}{% endgist %}

###if else

{% gist id="https://gist.github.com/roryl/11c5a5ab8cf061ab1621",file="if_else.cfm" %}{% endgist %}

###if else if else

{% gist id="https://gist.github.com/roryl/11c5a5ab8cf061ab1621",file="if_elseif_else.cfm" %}{% endgist %}

###switch

{% gist id="https://gist.github.com/roryl/11c5a5ab8cf061ab1621",file="switch.cfm" %}{% endgist %}

###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.

{% gist id="https://gist.github.com/roryl/11c5a5ab8cf061ab1621",file="loop_break.cfm" %}{% endgist %}

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

{% gist id="https://gist.github.com/roryl/11c5a5ab8cf061ab1621",file="loop_continue.cfm" %}{% endgist %}

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

{% gist id="https://gist.github.com/roryl/11c5a5ab8cf061ab1621",file="return.cfm" %}{% endgist %}

###for loop

{% gist id="https://gist.github.com/roryl/11c5a5ab8cf061ab1621",file="for.cfm" %}{% endgist %}

###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:

{% gist id="https://gist.github.com/roryl/11c5a5ab8cf061ab1621",file="for_in_array.cfm" %}{% endgist %}

###Do While loop

{% gist id="https://gist.github.com/roryl/11c5a5ab8cf061ab1621",file="do_while_loop.cfm" %}{% endgist %}

###While Loop {% gist id="https://gist.github.com/roryl/11c5a5ab8cf061ab1621",file="while_loop.cfm" %}{% endgist %}

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

See:

{% gist id="https://gist.github.com/roryl/11c5a5ab8cf061ab1621",file="loop.cfm" %}{% endgist %}

###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; {% gist id="https://gist.github.com/roryl/11c5a5ab8cf061ab1621",file="for_loop_label.cfm" %}{% endgist %}

####Nested For Loops with Labels This exits out of the child loop and continues after the parent loop {% gist id="https://gist.github.com/roryl/11c5a5ab8cf061ab1621",file="for_loop_label_nested.cfm" %}{% endgist %}

####Nested Loop tags with Label Labels are also possible with loop tags using the label attribute {% gist id="https://gist.github.com/roryl/11c5a5ab8cf061ab1621",file="loop_label_nested.cfm" %}{% endgist %}

####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:

{% gist id="https://gist.github.com/roryl/11c5a5ab8cf061ab1621",file="for_loop_label_nested_continue.cfm" %}{% endgist %}

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

{% gist id="https://gist.github.com/roryl/11c5a5ab8cf061ab1621",file="for_loop_label_nested_break.cfm" %}{% endgist %}

, produces:

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