See Also: Boolean Expressions, Execution Control Commands, For, Loop, Repeat, Until, Break, Boolean
Marks a block of code to be executed repeatedly depending on a condition.
While {Boolean-expression}
... commands ...
Loop
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.
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
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. And you may freely intermingle the various types of loops. But 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 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.