Looping DATA

From BeebWiki
Jump to: navigation, search

Programs that read data from DATA statements sometimes don't know how much data is present, and are written to read data until a terminating value is read. A typical loop to do this would be something like the following:

 REPEAT
   READ item$
   IF item$<>"*" THEN blah blah blah
 UNTIL item$="*"
 DATA cat,dog,fish,*

A neater way of writing this would be the following:

 READ item$
 REPEAT
   blah
   blah
   blah
   READ item$
 UNTIL item$="*"
 DATA cat,dog,fish,*

By reading the first item before entering the loop and fetching the next item before the loop returns the program only has one test. By removing the IF in the loop more processing can be done on each item in a neater manner. This is particularly useful if writing for a BASIC that doesn't have multi-line IFs.

The limitation of this method is that the data set must always have at least one item.

Jgharston (talk) 21:11, 3 July 2016 (UTC)