While...End While Statement

The While...End While statement acts according to a Boolean test expression. As long as the test expression evaluates to True, the statement will continue to iterate. The following example shows a simple While...End While loop.

Dim counter As Integer = 0
Dim total As Integer = 0
While counter < 10
   total = total + counter
   counter = counter + 1
End While

You can jump out of a While...End While loop by using an Exit statement, as shown in the following example.

In this example, a parameter specifies the weight per unit of some item. A while loop inside the load rule adds the weight to a total load (of up to 10 units). The load has a limit of 25. As additional unit weights are added, the total load is compared to the limit and a system message is generated. If the load exceed the limit, a warning is added to the message, and the iteration stops. Control passes to the End While statement.

Parameter Rule unitWeight As Integer = 5

  
Rule load As Number
   Dim counter As Integer = 1
   Dim limit As Number = 25.0
   Dim strItem As String = " item."
   While counter <= 10
      If counter > 1 Then
         strItem = " items."
      End If
      load = load + unitWeight
      If load > limit Then
         printValue("Warning: Load = " + stringValue(load) + _
            "kg for " + stringValue(counter) + strItem + " " + _
            stringValue(limit) + "kg limit exceeded!")
         Exit While
      Else
         printValue("Load = " + stringValue(load) + _
            "kg for " + stringValue(counter) + strItem)
      End If
      counter = counter + 1
   End While
End Rule
Output:
"Load = 5.000kg for 1 item."
"Load = 10.000kg for 2 items."
"Load = 15.000kg for 3 items."
"Load = 20.000kg for 4 items."
"Load = 25.000kg for 5 items."
"Warning: Load = 30.000kg for 6 items. 25.000kg limit exceeded!"
Note: See Exit Statement for more information.