Loop
Executes the appropriate loop type based on the provided attributes.
Component Signature
<bx:Loop array=[array]
item=[string]
index=[string]
to=[double]
from=[double]
step=[number]
file=[string]
list=[string]
delimiters=[string]
collection=[collection]
condition=[function]
query=[any]
group=[string]
groupCaseSensitive=[boolean]
startRow=[integer]
endRow=[integer]
label=[string]
times=[integer] />
Attributes
array
array
false
An Array object to iterate over. When specified, the loop will process each element
in the array. Can be used with item
and/or index
attributes.
Example: <bx:loop array="#myArray#" item="element">
item
string
false
Variable name to hold the current item value during iteration. Behavior varies by loop type:
Array loops: Contains the current array element
List loops: Contains the current list item
Collection loops: Contains the current collection key
Times loops: If no index specified, contains the current iteration number
Example: <bx:loop array="#colors#" item="color">
index
string
false
Variable name to hold the current index/position during iteration. Behavior varies by loop type:
Array loops: Contains the 1-based array index
Numeric range loops: Contains the current numeric value (required)
File loops: Contains the current line content (required)
Times loops: Contains the current iteration number
Example: <bx:loop from="1" to="10" index="i">
to
double
false
End value for numeric range loops. The loop continues while the index value
has not exceeded this value (considering step direction). Required when using
numeric range loops with from
and index
.
Example: <bx:loop from="1" to="100" step="5" index="i">
from
double
false
Starting value for numeric range loops. Defaults to 0 if not specified.
Used in conjunction with to
and index
for numeric iteration.
Example: <bx:loop from="10" to="1" step="-1" index="i">
step
number
false
Increment/decrement value for numeric range loops. Defaults to 1.
Positive values increment the index, negative values decrement.
Zero values are ignored to prevent infinite loops.
Example: <bx:loop from="0" to="20" step="2" index="even">
1
file
string
false
Absolute path to a text file to read line by line. Each iteration provides
one line of the file content in the index
variable. The file
is automatically closed when the loop completes. Requires index
attribute.
Example: <bx:loop file="/path/to/data.txt" index="line">
list
string
false
A delimited string to process item by item. Each item becomes available
through the item
or index
variable. Use with
delimiters
to specify custom separators.
Example: <bx:loop list="red,green,blue" item="color">
delimiters
string
false
Characters that separate items in the list
attribute.
Defaults to comma (",") if not specified. Multiple delimiter characters
can be specified.
Example: <bx:loop list="a
b
collection
collection
false
A Java Collection object (including BoxLang Structs) to iterate over.
For Structs, each iteration provides a key. Requires item
attribute
to specify the variable name for the current key.
Example: <bx:loop collection="#myStruct#" item="key">
condition
function
false
A function/closure that returns a boolean value. The loop continues
while this condition evaluates to true. The condition is evaluated
before each iteration.
Example: <bx:loop condition="#() => hasMoreData()#">
query
any
false
A Query object or variable name containing a query to iterate over.
Each iteration makes one row of the query available. Can be combined
with group
for grouped processing, or startRow
/
endRow
for range constraints.
Example: <bx:loop query="#employeeQuery#">
group
string
false
Comma-separated list of query column names to group by. When specified,
the loop processes data in groups, executing the body once per group
change rather than once per row. Enables nested looping for hierarchical
data processing. Requires query
attribute.
Example: <bx:loop query="#data#" group="department,manager">
groupCaseSensitive
boolean
false
Boolean flag controlling whether group comparisons are case-sensitive.
Defaults to false (case-insensitive). Only meaningful when
group
attribute is specified.
Example: <bx:loop query="#data#" group="name" groupCaseSensitive="true">
false
startRow
integer
false
1-based starting row number for query loops. Only rows from this
position onward will be processed. Must be 1 or greater.
Example: <bx:loop query="#data#" startRow="10" endRow="20">
endRow
integer
false
1-based ending row number for query loops. Processing stops after
this row. Must be 1 or greater and typically greater than startRow
.
Example: <bx:loop query="#data#" startRow="1" endRow="50">
label
string
false
Optional label for the loop, used with break and continue statements
to control nested loop execution. Allows targeting specific loops
in complex nested structures.
Example: <bx:loop label="outerLoop" array="#data#">...<bx:break label="outerLoop">
times
integer
false
Number of iterations to perform. Creates a simple counting loop
from 1 to the specified number. Can be used with item
or index
to access the current iteration number.
Example: <bx:loop times="5" index="i">
Examples
For Loop (Script Syntax)
General Purpose Loop
for( i = 1; i <= 10; i++ ) {
writeOutput( i );
}
Result: 12345678910
For Loop using Bx:loop Tag
General Purpose Loop
<bx:loop index="i" from="1" to="10">
<bx:output>#i#</bx:output>
</bx:loop>
Result: 1 2 3 4 5 6 7 8 9 10
Loop Over an Array (Script Syntax)
Array Loop
myArray = [
"a",
"b",
"c"
];
// For Loop By index
for( i = 1; i <= arrayLen( myArray ); i++ ) {
writeOutput( myArray[ i ] );
}
// By For
for( currentIndex in myArray ) {
writeOutput( currentIndex );
}
// By arrayEach()
myArray.each( ( Any element, Any index ) => {
writeOutput( element & " : " & index );
} );
Bx:loop over an Array
Array Loop
<bx:set myArray = [
"a",
"b",
"c"
] >
<!--- By index --->
<bx:loop index="i" from="1" to="#arrayLen( myArray )#">
<bx:output>#myArray[ i ]#</bx:output>
</bx:loop>
<!--- By array --->
<bx:loop index="currentIndex" item="currentItem" array="#myArray#">
<bx:output>#currentIndex#</bx:output>
<bx:output>#currentItem#</bx:output>
</bx:loop>
Loop over a Struct (Script Syntax)
Struct Loop
myStruct = {
NAME : "Tony",
STATE : "Florida"
};
// By struct
for( currentKey in myStruct ) {
writeOutput( "<li>#currentKey# : #myStruct[ currentKey ]#</li>" );
}
// By structEach()
myStruct.each( ( Any key, Any value ) => {
writeOutput( "<li>#key# : #value#</li>" );
} );
Bx:loop over a Struct
Loop over a Struct using the collection and item arguments of bx:loop.
<!--- Define our struct --->
<bx:set myStruct = {
NAME : "Tony",
STATE : "Florida"
} >
<!--- By struct --->
<bx:loop item="currentKey" collection="#myStruct#">
<bx:output><li>#currentKey# : #myStruct[ currentKey ]#</li></bx:output>
</bx:loop>
Bx:loop over a Struct
Loop over a Struct using the collection, index and item arguments of bx:loop.
<!--- Define our struct --->
<bx:set myStruct = {
NAME : "Tony",
STATE : "Florida"
} >
<!--- By struct --->
<bx:loop item="currentItem" collection="#myStruct#" index="currentKey">
<bx:output><li>#currentKey# : #currentItem#</li></bx:output>
</bx:loop>
Loop over a List (Script Syntax)
List Loop
// Define our list
myList = "a, b, c";
// By array
for( item in listToArray( myList, "," ) ) {
writeOutput( item );
}
// By listEach()
myList.each( ( Any element, Any index ) => {
writeOutput( element & " : " & index );
}, "," );
Bx:loop over a List
List Loop
<!--- Define our list --->
<bx:set myList = "a, b, c" >
<!--- By list --->
<bx:loop index="item" list="#myList#">
<bx:output>#item#</bx:output>
</bx:loop>
<!--- By array --->
<bx:loop index="currentIndex" array="#listToArray( myList, "," )#">
<bx:output>#currentIndex#</bx:output>
</bx:loop>
Loop over a Query with Grouping (Script Syntax)
Query Loop use grouping
q = queryNew( "pk,fk,data", "integer,integer,varchar", [
[
1,
10,
"aa"
],
[
2,
20,
"bb"
],
[
3,
20,
"cc"
],
[
4,
30,
"dd"
],
[
5,
30,
"ee"
],
[
6,
30,
"ff"
]
] );
bx:loop query=q group="fk" {
writeOutput( "<strong>#fk#</strong><br />" );
bx:loop {
writeOutput( " #pk#:#data#<br />" );
}
writeOutput( "<hr>" );
}
Bx:loop over a Query
Query Loop
<!--- Define our query --->
<bx:set platform = [
"Adobe ColdFusion",
"Railo",
"Boxlang"
] >
<bx:set myQuery = queryNew( " " ) >
<bx:set queryAddColumn( myQuery, "platform", "VARCHAR", platform ) >
<!--- By row index --->
<bx:loop index="i" from="1" to="#myQuery.RECORDCOUNT#">
<bx:output><li>#myQuery[ "platform" ][ i ]#</li></bx:output>
</bx:loop>
<!--- By group --->
<bx:loop query="myQuery" group="platform">
<bx:output><li>#platform#</li></bx:output>
</bx:loop>
While Loop (Script Syntax)
Pre-Condition Loop This form of loop evaluates a single condition at the beginning of each iteration, and continues to loop whilst the condition is true
while (condition) {
// statements
}
Do While Loop (Script Syntax)
Post-Condition Loop This form of loop evaluates a single condition at the beginning of each iteration, and continues to loop whilst the condition is true
do {
// statements
}
while (condition);
Last updated
Was this helpful?