ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Iterator overflow in for loop

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Iterator overflow in for loop

    I am being braindead. Why does this crash with MCH1210: Receiver value too small to hold result. ??

    After going through what should be the final iteration with the iterator variable = 999, it crashes on the FOR statement when trying to increase the iterator again. WTH?

    The iterator variable is not being changed inside the the loop.

    Code:
    d iterVar         s              3s 0
    
    c                   for       iterVar     = 1 to 999
    ...
    c                   endfor

  • #2
    Because the loop with end when iterVar is 1000. But 3s 0 cannot hold that 4 digit value. Define it as 5i 0 or 10i 0.

    Ringer

    Comment


    • #3
      I had this same issue several months ago. The reason the FOR statement is crashing is because it adds the increment value (in this case 1) to the variable before it checks to see if the variable is greater than the maximum limit. So if iterVar is 998, it adds 1 to it giving 999, it checks to see if 999 is greater than 999, it's not so execute the loop, it adds 1 to iterVar and crashes because 1000 can't be stored in a decimal variable that is 3 digits long. The fix is to make iterVar a length of 4.

      Comment

      Working...
      X