Skip to content

While

See Also: Boolean Expressions, Execution Control Commands, For, Loop, Repeat, Until, Break, Boolean

Purpose

Marks a block of code to be executed repeatedly depending on a condition.

Syntax

While {
    Boolean-expression
}
...
commands
...
Loop

What It Does

The While command executes when the expression is true. If the expression is false, execution skips to the first line following the next loop statement. If the expression is true, execution continues with the command following the While command. When Loop is executed, execution returns to the While command, and the test is evaluated as before.

String sName
Get FirstName to sName
While (sName <> "")
    Send CreateLetter sName
    Get NextName to sName
Loop

A While ... Loop block will not execute, even the first time, when the test is false.

While can be driven by a single Boolean value.

Boolean bDone
Move False to bDone
While (not (bDone))
    Send GetNextRecord
    Move (Customer.Recnum = 0) to bDone
Loop

In this example, the While loop will be executed until the value of Customer.Recnum is zero. The loop will always be executed at least once.

Sample

This sample separates a string into words by separating character groups (words) before and after any space in the text.

Function SplitStringIntoWords String sText Returns String[]
    String sWord
    String[] Words
    Integer iWordCount iSpacePos

    // Repeat as long as sText contains data
    While (sText <> "")
        // Find the first/next space
        Move (Pos(" ", sText)) to iSpacePos

        // If a space was found
        If (iSpacePos > 0) Begin
            // Take the left part
            Move (Left(sText, iSpacePos - 1)) to sWord
            // Move the remainder to the text variable
            Move (Right(sText, Length(sText) - iSpacePos)) to sText
        End
        // If no more spaces are found
        Else Begin
            // Make word equal to the text
            Move sText to sWord
            // Empty text to stop the loop
            Move "" to sText
        End

        Move sWord to Words[iWordCount]
        Increment iWordCount
    Loop

    Function_Return Words
End_Function

Procedure OnClick
    String sText
    String[] Words
    Move "Split this text into words" to sText
    Get SplitStringIntoWords sText to Words
End_Procedure

Notes

  • You may exit from the middle of a loop by using break. If your loop is within a function or procedure (which it typically is), it may be terminated with function_return or procedure_return, which also terminate the function or procedure.

  • Statements inside blocks are usually indented to make the blocks more readable.

  • You may terminate a block which begins with While with any loop-terminating command, including Loop and Until (whose syntax provides for conditional control).

  • You may place While ... Loop blocks inside other looping blocks, up to a limit of 40. You may freely intermingle the various types of loops. However, you may not overlap them—you must end the innermost block first, then the next innermost, and so on.

  • You may make While subject to "compound" conditionals.

Number nWealth
Boolean bRich bSingle
Move True to bRich
Move True to bSingle
While (bRich AND bSingle)
    Send Party
    Get CurrentWealth to nWealth
    Move (nWealth > 500000) to bRich
    Get StillSingle to bSingle
Loop

In this example, when both Booleans bRich and bSingle are true, the While loop executes the message Party. The statuses of bRich and bSingle are checked and reset after each party, and the loop re-executes until either bRich or bSingle is false.

  • A variable or expression with a value of zero (0) is the test equivalent of false. A variable or expression with a value other than zero is the test equivalent of true.

  • You may apply if commands ("conditionals") to both the While statement and the While loop ending statement.

However, conditionals which you apply before While are outside the loop. Conditionals in this position affect only the first execution of the block, not repeat executions, and they will supersede the While expression evaluation. The expression will be evaluated only when the conditional is true, so the While ... Loop block will be executed only when the conditional is true and the test is true.

Conditionals which you apply after the While statement, or to the loop ending statement, are inside the loop. They will be evaluated each time the loop is executed.