The while statement is used create loops in a DM and C++ program. It has the general form:
while ( Expression )
Statement
where,
expression -- Defines the condition that controls the loop and can be any valid expression.
statement -- A single statement or a block of statements.
In the program, the Expression is first evaluated. If the condition is true, the statement is performed. When the condition becomes false, then the
program control passes to the line immediately following the loop.
And then, the Expression is evaluated again and the cycle is repeated. This continues until Expression evaluates to false (0). See the simple example. However, similar to FOR loop, the WHILE checks the conditional expression at the top of the
loop, which means that the loop code may not execute at all if nothing is true.
Table 1141a. WHILE statement.
while (condition) action |
Loop statement: to create loops in a program so that the actions are repeated as long as the condition is completed, i.e., the expression evaluates to 0. |
Example1
Example2 |
while (condition)
{
action1
action2
...
} |
Loop statement: to create loops in a program so that the actions are repeated as long as the condition is completed |
Example1
Example2
Example3
Example4 |
Table 1141b. Examples of WHILE statement.
Script example |
Result |
number EMi = 32;
while(EMi) {
result (EMi + "\n")
EMi++;
}
Script |
Print numbers starting with 32. |
while(random() != 0.937839 )
{
result ( "Trying to random number to match 0.937839.\n")
}
Script |
In the body of the while loop, there need not be any statements at all. Here shows an example.
The script tries to find a random number, between 0 and 1, which is equal to 0.937839.
The program stops once this number is found. |
TagGroup GlobalSinoDialog, EMItems
GlobalSinoDialog = DLGCreateDialog( "Number selection", EMItems )
TagGroup MyOptions = DLGCreateRadioList( 1947) // "1947" here says the default selection is the one with the same number below.
MyOptions.DLGAddRadioItem( "1947", 1947 )
MyOptions.DLGAddRadioItem( "1967", 1967 )
MyOptions.DLGAddRadioItem( "1987", 1987)
EMItems.DLGAddElement( DLGCreateLabel( "Please select a number:" ) )
EMItems.DLGAddElement( MyOptions )
if ( !Alloc( UIframe ).Init( GlobalSinoDialog ).Pose() ) exit (0)
// End of choice setup
number EMInputGuess = MyOptions.DLGGetValue()
// WHILE statement is below
while(EMInputGuess>1966 && EMInputGuess<1968)
{
EMInputGuess--;
Result (EMInputGuess + "\n")
}
Result ("Done! \n\n") Script |
WHILE statement with input dialogs |
In the class of WHILE loops, another very useful loop in C++ is do-while loop, in which the do-while loop checks its condition at the bottom of the loop. Therefore, the do-while loop will always execute at least once. The form of the
do-while loop is normally:
do {
statements;
} while(expression)
Unfortunately, DM does not support do-while loop.
In many cases, WHILE statement can be replaced by FOR statements (refer to comparison between for and while statements).
|