 # For loop behaving badly ?

Hi all,

I’m programming an interface that prints labels for shipment. Let me explain what should happen:

Client orders:

• less than 5 items >>> a label is printed for each item
• more than 5 items >>> ONE label is printed for all items

However there is a maximum of 27 items that can be shipped on one pallet.
So if there is an order of say 70 items:

• one label for 27 items
• second label for 27 items
• third label for the remaining 16 items

``````            // If amount < intBulkTreshold print label(s) for each
if(arrOrderData[p] < intBulkTreshold){

for (var ap:int = 1; ap <= arrOrderData[p] * arrOrderData[p]; ap++){
mcLabel.txtColliNumber.text = intLabelNr + "/" + intLabelCount;
intLabelNr++;
}

}else{

var intSplitCount:int = Math.ceil(arrOrderData[p] / intMaxPerPallet)
var intSplitLabelNr:int = 1;

for(var sp:int = 1; sp <= intSplitCount; sp++)
trace("splitcount=" + intSplitCount);

trace("intSplitLabelNr=" + intSplitLabelNr);

// if amount is less than maxperpallet
if(intSplitCount == 1){
mcLabel.txtColliCount.text = "This is one label for " + arrOrderData[p] + "x ArtNr. " + arrOrderData[p];

mcLabel.txtColliNumber.text = intLabelNr + "/" + intLabelCount;
intLabelNr++;
trace("A");
}

// as long as it's not the last label
if(intSplitLabelNr < intSplitCount ){
mcLabel.txtColliCount.text = "This is one label for " + intMaxPerPallet + "x ArtNr. " + arrOrderData[p];
intSplitLabelNr++;

mcLabel.txtColliNumber.text = intLabelNr + "/" + intLabelCount;
intLabelNr++;
trace("B");
}

if(intSplitLabelNr == intSplitCount && arrOrderData[p] > intMaxPerPallet){

var intRest:int = arrOrderData[p] - ( (intSplitCount-1)*intMaxPerPallet )

mcLabel.txtColliCount.text = "This is one label for " + intRest + "x ArtNr. " + arrOrderData[p];
intSplitLabelNr++;

mcLabel.txtColliNumber.text = intLabelNr + "/" + intLabelCount;
intLabelNr++;
trace("C");
}

}    //end for
} //end if
``````

[/code]

There is some error in the printing of labels:

When amount =< 54 all goes well.
If amount > 54 something goes wrong.

While debuging the problem using trace(); I notice something strange. And this is my main problem/question for now:

Running the above with amount=70 outputs:

```splitcount=3 splitcount=3 splitcount=3 intSplitLabelNr=1 B```

Main question for this post
(after a long intro )

The problem seems to be in this loop:

[code]var intSplitCount:int = Math.ceil(arrOrderData[p] / intMaxPerPallet)
var intSplitLabelNr:int = 1;

for(var sp:int = 1; sp <= intSplitCount; sp++){

}
[/code]

This is what confuses me. I have two traces on two consecutive lines:

```trace("splitcount=" + intSplitCount); trace("intSplitLabelNr=" + intSplitLabelNr);```

The first trace (splitcount) outputs three times as it should
The second trace (intSplitLabelNr) only outputs once ??

Could anyone shed some light on why this is happening ?

Yep you’re right, I spent like 30 minutes figuring out where the missing curly brace should go 