while/break/continue (in ruleflow)

The while keyword executes a loop.

Purpose

This ruleflow conditional statement is used to execute a loop while a Boolean expression value remains true.

Context

Ruleflow body

Syntax

while (test)
   {ruleflowStatement}

Description

The test argument can be any legal test, as in the Java™ programming language. As long as this value remains true, the statements inside the while block are executed. Any ruleflow statement can be in a while block. When the value becomes false, the engine stops executing the while block and resumes ruleflow execution after the while statement.

A break statement can interrupt a while loop. When the engine encounters a break statement, the while loop is interrupted and ruleflow execution continues after the while statement.

You can include a continue statement in a while loop. When the engine encounters a continue statement, ruleflow execution returns to the while statement again, reevaluates the test, and continues ruleflow execution according to this value.

Example

ruleset Connect4
{
   // The latest move
   int turn;
   // Who's the winner, what is the connect4, and any other reason
   // to end the game.
   int winner = Constants.None;
   Connect4 connect4;
   boolean ending = false;
};
flowtask main
{
   body =
   {
      while(!ending)
      {
         if (turn == Constants.Player1) ChooseMovePlayer1;
         else ChooseMovePlayer2;
         CheckMove;
         if (ending) break;
         UpdateDistance;
         ExpandObjects;
         DetectConnect4;
         if (ending) break;
         DetectGridFull;
         if (ending) break;
         ChangeTurn;
      }
      EndOfGame;
   }
}

This example illustrates a while statement. The while block is executed while the ending variable value remains false (because then !ending remains true).

If the ending Boolean value becomes true, a break statement can interrupt the loop.