NAME

do...loop - general condition loop statement

SYNOPSIS

do [Condition]



(loop statements)



loop [Condition]

DESCRIPTION

The do...loop statement construct forms a conditional loop similar to that of while...wend but is more general.  The statements within the loop are executed repeated because control is passed from the loop statement back up to the do statement.  The cycle continues in this way while or until the Condition is met.  It is also possible for control to be transferred into or out of the loop other means, such as a go to statement.  Do...loops may be nested. 

The Condition consists a word, either while or until, followed by a numeric expression.  Each time through the loop, the numeric expression is evaluated and the looping continues while the expression is true, if the word while appears, or until the expression is true, if the word until appears.  If the result of the evaluation requires the loop to be terminated, execution proceeds with the statement following the loop statement. 

The Condition may appear on either the do statement or the loop statement, but not both.  The Condition may be omitted from both statements, which produces an infinite loop: the loop is never terminated (except by other means). 

If the Condition appears on the do statement, then the numeric expression is evaluated "at the top of the loop" and if the Condition appears on the loop statement, then the numeric expression is evaluated "at the bottom of the loop." Logically, these scenarios are virtually identical: in either case, the expression is evaluated between iterations of the loop statements.  The difference occurs only when control is transferred to the do statement the first time (rather by looping from the loop statement).  Then the difference appears as which occurs first: the evaluation of the expression or the execution of the loop statements.  If the Condition is placed on the do statement, then the expression is evaluated first.  If the Condition is placed on the loop statement, then the loop statements are executed first. 

It is worth noting that if the Condition uses the word while and appears on the do statement, then this functions the same as the while...wend statement, if the numeric expression is the same. 

EXAMPLE

One might build a simple predictive parser to parse a comma separated list of items from a file, or other source.  The characters are actually read and divided into simple units called tokens by the function "usrlex" which returns a number associated with the type of token read and also saves the characters of the token in a buffer.  There is also a subprogram called "storeitem" which actually interprets the text of an "item" token and stores this value for internal use by the program. 

The purpose of the following example is to show how the structure that is a comma separate list might be represented (indeed embedded) in the control structure of a program, called a parser:

		do
			ltype = usrlex
			if ltype <> ITEM then_
				print "syntax error"
			call storeitem
			ltype = usrlex
		while ltype = COMMA
At the end of this loop, one addition token will have been read, and its type stored in "ltype". 

SEE ALSO

while...wend, goto

USAGE NOTES

Testing a loop at the bottom is rarely appropriate; however, these situations do occasionally arise.  Bottom tested loops do execute slightly faster and if a loop is known to always execute once, bottom testing saves the time required to evaluate the numeric expression the first time.  But these uses of bottom testing produce a false economy: usually the code needs rewriting when a reason is found not to execute the loop the first time.  Also, the behavior of an algorithm with zero inputs can be very informative, but this opportunity is almost always lost when the algorithm contains a bottom tested loop.  When it is felt that a loop is best bottom tested, it should be coded again with the loop top tested and the merits of each approach compared objectively. 

from The Basmark QuickBASIC Programmerís Manual by Lawrence Leinweber