Lesson 4 -- While statements

While Statement

The WHILE statement is another method of controlling program flow. The syntax for a WHILE statement is:

ex4-12.gif (1609 bytes)
figure 4-12

The WHILE statement evaluates the <boolean_eval> at the top of the loop. If the expression evaluates to TRUE then the <statement(s)> are evaluated. It is extremely important to note that if the <boolean_eval> evaluates to FALSE then the <statement(s)> are never executed. The <statement(s)> are repeated until the <boolean_eval> evaluates to FALSE. Once the expression is FALSE the code immediately following the END keyword is executed.

You can think of the WHILE statement as a combination of the IF and FOR statements. IF the <boolean_eval> is TRUE the <statement(s)> are executed just like an IF statement. Likewise, the <statement(s)> can be repeated much like in a FOR loop.

Here is an example.

ex4-13.gif (9286 bytes)
figure 4-13

Complete listing for MODTEST.DCS.  Click the image to download the source code. If your browser prompts you to run the file or save it to a disk, select save to disk. If you create a file association for *.DCS files this file will automatically be opened in your editor.

This is a complete program that you may compile and experiment with.

Notice the use of the WHILE statement to allow this loop to execute until the user decides to stop. Here is how this one works. First we declare the BOOLEAN variable done. The done variable is set to FALSE just before entering the WHILE loop. The WHILE loop is evaluated and at first done will be FALSE so NOT DONE evaluates to TRUE. The NOT keyword simply flips a BOOLEAN value (TRUE becomes FALSE. FALSE becomes TRUE) The actual value of done is not changed it is simply evaluated as its opposite value.

Once in the loop the program asks for some numbers then displays the MOD of the 2 numbers. The WHILE statement doesn't come back into play until the IF statement at the end of the loop. The user is asked if the loop should end. The ANSWER function is used. ANSWER will display two buttons on the menu with YES/NO choices.

IF the user selects NO when asked whether to continue done is set to TRUE. The loop then returns to the top to be reevaluated. If the user has decided not to continue then done is now TRUE and NOT done will evaluate to FALSE causing the loop to end and in this case the macro ends as well. If the user wishes to continue then done remains FALSE and the loop repeats.

Common errors using WHILE loops are not initializing the <boolean_eval> to a true value causing the loop to never be executed. This may be what you want to occur but it may also be an error. Be sure what you want to happen and handle your <boolean_eval> accordingly. The opposite is also possible and that is never providing a way for the loop to exit. This will cause your macro to repeat endlessly. Always provide a way for the WHILE loop to terminate or your macro may appear hung.

Use a WHILE loop whenever you MAY want to execute some code AND when you are not sure how many times the statements will need to be executed.

< Previous Lesson   |   Next Lesson >
DCAL Tutorial Contents