PxPlus User Forum

Main Board => Discussions => Programming => Topic started by: Ken Sproul on August 20, 2019, 03:01:45 PM

Title: Question about the end clause of a select / next record loop
Post by: Ken Sproul on August 20, 2019, 03:01:45 PM
Is an expression used in the end clause of a select / next record loop evaluated at every iteration?
Title: Re: Question about the end clause of a select / next record loop
Post by: keith.mcbride on August 20, 2019, 04:32:17 PM
Ken, Interesting question.  I wrote the following program:

0100 BEGIN
0110 OPEN (1)"*MEMORY*"
0120 OPEN (2)"/tmp/test"
0200 FOR i=1 TO 10
0205 WRITE (1,KEY=STR(i:"00"))i
0210 NEXT i
0215 c=10
0300 SELECT i FROM 1 BEGIN "" END STR(c:"00")
0305 PRINT (2)"I=",i," C=",c
0310 c-=1
0320 NEXT RECORD

If the END is only evaluated once at the beginning then I expect it to read all 10 records, ignoring the new value of C
If the END is evaluated each iteration, then I would expect the program to stop reading somewhere around record 6 or 5.

The results:
I= 1 C= 10
I= 2 C= 9
I= 3 C= 8
I= 4 C= 7
I= 5 C= 6
I= 6 C= 5
I= 7 C= 4
I= 8 C= 3
I= 9 C= 2
I= 10 C= 1

It would appear that the end is only evaluated once at the beginning of the SELECT

Keith
Title: Re: Question about the end clause of a select / next record loop
Post by: Devon Austen on August 20, 2019, 04:49:46 PM
The BEGIN and END on a SELECT are evaluated once at the beginning.
Title: Re: Question about the end clause of a select / next record loop
Post by: Ken Sproul on August 20, 2019, 05:21:36 PM
Thanks Keith and Devon.  I thought at least the end clause was evaluated each time.  This is actually good news, because I was trying to avoid complex expressions in the end clause for performance reasons, but since it's only evaluated once, I don't have to worry about that.