For Each x = 1 to 10

kolonuk

Member
Hi All,

This is just something I noticed in passing (caused me a real headache!), so just throught I'd share it.

I only needed the first 10 or less rows of a table, so
Code:
    for each table
        /* where clause */
    x = 1 to 10
    :
    /* do processing */
    end.

At the end, I checked x to find out how many records were found. If exactly 10 records would be found in the table anyway without the "x = 1 to 10", x would be equal to 10.

It was then that I noticed that if more than 10 records were found (20, 30 etc.), x would be 11. Is it just me, or should x still be 10 regardless if there are more records available?



Example to test:
Code:
/* setup environment */
def var x as inte.
def temp-table t1
    field t1_inte as inte
    index t1_io t1_inte
    .

/* build temp-table */
do x = 1 to 10
:
    create t1. t1_inte = x.
end.
/* example 1 - I expect x to be 10 when foreach is done */
run i-foreach.
display x with frame f_1.


/* add one more entry to temp table */
create t1. t1_inte = 11.
/* example 2 - i stil expect x to be 10, but it isn't... */
run i-foreach.
display x with frame f_2.


/* procedure containing "for each" */
procedure i-foreach
:
    for each t1
    x = 1 to 10
    :
    end.
end procedure.
 

Marian EDU

Member
it's just you... the variable will be incremented each time it goes in the loop and it pop-out when is greater than the end value as for any 'do i = 1 to N' statement, thing is that if less than N records exists in that table the 'for' block will come to it's end, this will leave the counter pointing to the exact number of records found.
 
Top