There are several different types of for loops. For more info please see the docs for cfloop.
The following for loop has been supported since the initial version of cfscript.
array = [3,2,1];
for (i=1; i <= arrayLen(array); i++) {
writeOutput(array[i]);
}
The above would output 321
struct = {a=1,b=2};
for (key in struct) {
writeOutput(key);
}
The above outputs AB
Using loop you can loop over the key and values (Lucee5+)
loop collection=st key="key" value="value" {
writeOutput(key & " " & value);
}
cars = ["Ford","Dodge"];
for (car in cars) {
writeOutput(car);
}
The above example would output FordDodge
For in support for native Java arrays was added in CF10+
Using loop you can track the index and the value (Lucee5+)
loop collection=cars index="i" value="value" {
writeOutput("#i#: #value#");
}
fruits = "apple,orange,banana";
for (fruit in fruits) {
writeOutput(fruit);
}
The above example would output appleorangebanana
query = queryNew("name", "varchar", [
["apple"],
["banana"],
["orange"]
]);
for (row in query) {
writeOutput("#query.currentRow# - #row.name#<br>");
}
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"]
]);
cfloop(query=q, group="fk") {
writeOutput("<strong>#fk#</strong><br />");
cfloop() {
writeOutput(" #pk#:#data#<br />");
}
writeOutput("<hr>");
}
filePath = getCurrentTemplatePath();
cfloop(file=filePath, index="chars", characters=16, charset="UTF-8") {
writeOutput(chars); // outputs the contents of this file
}
from = now();
to = dateAdd("d", 7, from);
for (date=from; dateCompare(date, to, "d") <= 0; date = dateAdd("d", 1, date)) {
writeOutput(dateTimeFormat(date, "yyyy-mm-dd HH:nn:sstt") & "<br>");
}
cars = ["Ford","Dodge"];
for (car in cars) {
if (car == "Ford") {
continue;
}
writeOutput(car);
}
cars = ["Ford","Dodge"];
for (car in cars) {
if (car == "Ford") {
break;
}
writeOutput(car);
}