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

[code]// ADDING LABELS

            // If amount < intBulkTreshold print label(s) for each 
            if(arrOrderData[p][0] < intBulkTreshold){
                
                for (var ap:int = 1; ap <= arrOrderData[p][0] * arrOrderData[p][16]; ap++){
                    mcLabel.txtColliNumber.text = intLabelNr + "/" + intLabelCount;
                    myPrintJob.addPage(mcLabel,OutputAreaA4A3);
                    intLabelNr++;
                }
                
            }else{
                
                var intSplitCount:int = Math.ceil(arrOrderData[p][0] / 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][0] + "x ArtNr. " + arrOrderData[p][1];
                        
                        mcLabel.txtColliNumber.text = intLabelNr + "/" + intLabelCount;
                        myPrintJob.addPage(mcLabel,OutputAreaA4A3);
                        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][1];
                        intSplitLabelNr++;
                        
                        mcLabel.txtColliNumber.text = intLabelNr + "/" + intLabelCount;
                        myPrintJob.addPage(mcLabel,OutputAreaA4A3);
                        intLabelNr++;
                        trace("B");
                    }

                    if(intSplitLabelNr == intSplitCount && arrOrderData[p][0] > intMaxPerPallet){
                        
                        var intRest:int = arrOrderData[p][0] - ( (intSplitCount-1)*intMaxPerPallet )
                        
                        mcLabel.txtColliCount.text = "This is one label for " + intRest + "x ArtNr. " + arrOrderData[p][1];
                        intSplitLabelNr++;
                        
                        mcLabel.txtColliNumber.text = intLabelNr + "/" + intLabelCount;
                        myPrintJob.addPage(mcLabel,OutputAreaA4A3);
                        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 :slight_smile: )

The problem seems to be in this loop:

[code]var intSplitCount:int = Math.ceil(arrOrderData[p][0] / 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 ?

Thanks for reading !

You need some curly braces enclosing the block of code that you want looped over. Without them, only the next line is looped.

1 Like

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

Everything works as it should now