开发者

Is it possible to have two indexes in a coldfusion for-loop?

开发者 https://www.devze.com 2023-04-09 14:14 出处:网络
<cfscript> for(firstIndex = 1, secondIndex = 1; firstIndex > 10, secondIndex > 5; firstIndex++, secondIndex++)
<cfscript>
    for(firstIndex = 1, secondIndex = 1; firstIndex > 10, secondIndex > 5; firstIndex++, secondIndex++)
        WriteOutput('First Index:' & firstIndex & 'Second Index:' & secondIndex & '<br>');
</cfscript>

obviously this code doesn't work, but is the开发者_如何学Pythonre a way to do this in CF or do I just have to create my own second variable in the loop and do

if(secondIndex > 5)
    break;
secondIndex++;


Loops in CF may not have more that one index. The quickest way to do what you want is use a conditional loop:

<cfscript>
    firstIndex = 1;
    secondIndex = 1;
    while (firstIndex < 5 && secondIndex < 10)
    {
        WriteOutput('First Index:' & firstIndex & ' Second Index: ' & secondIndex & '<br/>');
        firstIndex++;
        secondIndex++;  
    }
</cfscript>

BTW, your > signs will want to be < that signs--otherwise your above code will never iterate through a single loop.


Not like you're doing it, no. Use a nested loop.

0

精彩评论

暂无评论...
验证码 换一张
取 消