Using WHILE … DO Statements

From InterBase

Go Up to The Procedure Body


WHILE DO is a looping statement that repeats a statement or block of statements as long as a condition is true. The condition is tested at the start of each loop. WHILE DOuses the following syntax:

WHILE (<condition>) DO
<compound_statement>
<compound_statement> = {<block> |
statement;}

The <compound_statement> is executed as long as <condition> remains TRUE.
A block is one or more compound statements enclosed by BEGIN and END.

For example, the following procedure uses a WHILE DO loop to compute the sum of all integers from one up to the input parameter, I:

CREATE PROCEDURE SUM_INT (I INTEGER) RETURNS (S INTEGER)
AS
BEGIN
s = 0;
WHILE (i > 0) DO
BEGIN
s = s + i;
i = i - 1;
END
END ;

If this procedure is called from isql with the command:

EXECUTE PROCEDURE SUM_INT 4;

then the results are:

S
==========
10

Advance To: