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

While 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 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 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

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.