-
3
- #1
Mike Lewis
Programmer
There was a thread here last week about the behaviour of SCAN loops - in particular, certain traps that developers might fall into. The entire thread was removed by Tek Tips management because of offensive language by the OP.
Despite that, the thread did contain some useful points, and I thought it would be worth continuing the discussion. In particular:
1. It is rarely necessary to test for EOF() within a SCAN loop. In other words, code like this is rarely necessary:
By definition, the the SCAN loop will terminate at the end of file.
2. A SCAN loop on an empty table does nothing. It will simply go straight from the SCAN to the ENDSCAN.
3. The behaviour of a SCAN loop on a table that only contains deleted records will depend on the setting of DELETED. With DELETED ON, it is the same as if the table was empty. With DELETED OFF, the records are processed as usual.
4. Point 1 does not necessarily apply if the loop includes one or more SKIPs. If the SKIPs take you beyond EOF, there is no error generated.
5. The SCAN loop always starts at the top of the table. There is never any need to do this:
6. I don't think this was mentioned in the thread, but it's worth remembering that SKIP loops will operate on the correct alias, even if you change the work area within the loop. In other words, in code like this:
the final SELECT is unnecessary. The SCAN will automatically apply the correct alias when looping back to the start.
I hope this is of interest. Feel free to add your comments and suggestions (but without the insults and personal attacks, of course).
Mike
__________________________________
Mike Lewis (Edinburgh, Scotland)
Visual FoxPro articles, tips and downloads
Despite that, the thread did contain some useful points, and I thought it would be worth continuing the discussion. In particular:
1. It is rarely necessary to test for EOF() within a SCAN loop. In other words, code like this is rarely necessary:
Code:
SCAN
IF EOF()
EXIT
ENDIF
ENDSCAN
By definition, the the SCAN loop will terminate at the end of file.
2. A SCAN loop on an empty table does nothing. It will simply go straight from the SCAN to the ENDSCAN.
3. The behaviour of a SCAN loop on a table that only contains deleted records will depend on the setting of DELETED. With DELETED ON, it is the same as if the table was empty. With DELETED OFF, the records are processed as usual.
4. Point 1 does not necessarily apply if the loop includes one or more SKIPs. If the SKIPs take you beyond EOF, there is no error generated.
5. The SCAN loop always starts at the top of the table. There is never any need to do this:
Code:
GO TOP
SCAN
...
ENDSCAN
6. I don't think this was mentioned in the thread, but it's worth remembering that SKIP loops will operate on the correct alias, even if you change the work area within the loop. In other words, in code like this:
Code:
SELECT Table1
SCAN
&& do something
SELECT Table2
&& do something with another table
SELECT Table1
ENDSCAN
the final SELECT is unnecessary. The SCAN will automatically apply the correct alias when looping back to the start.
I hope this is of interest. Feel free to add your comments and suggestions (but without the insults and personal attacks, of course).
Mike
__________________________________
Mike Lewis (Edinburgh, Scotland)
Visual FoxPro articles, tips and downloads