# For loop behaving badly ?

**URL:** <https://forum.kirupa.com/t/for-loop-behaving-badly/634657>\
**Category:** flash\
**Created:** [May 21, 2016, 7:52pm UTC](https://forum.kirupa.com/t/for-loop-behaving-badly/634657 "2016-05-21T19:52:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![McLucky](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@McLucky](https://forum.kirupa.com/u/McLucky)\
**Post date:** [May 21, 2016, 7:52pm UTC](https://forum.kirupa.com/t/for-loop-behaving-badly/634657/1 "2016-05-21T19:52:55Z")

</div>

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 🙂 )

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 !

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [May 22, 2016, 7:08am UTC](https://forum.kirupa.com/t/for-loop-behaving-badly/634657/2 "2016-05-22T07:08:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![McLucky](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@McLucky](https://forum.kirupa.com/u/McLucky)\
**Post date:** [May 22, 2016, 9:48am UTC](https://forum.kirupa.com/t/for-loop-behaving-badly/634657/3 "2016-05-22T09:48:27Z")

</div>

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

Everything works as it should now
