The WHILE loop in ProBuilder language is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The loop will continue to execute as long as the condition remains true. This is particularly useful for iterating through data or performing repetitive tasks until a specific condition changes.
Syntax:
WHILE condition DO
// Statements to execute
WEND
Example:
i = 0
count = 0
WHILE i <> 11 DO
i = i + 1
count = count + 1
WEND
RETURN count
In this example, the WHILE loop starts with i initialized to 0. The loop continues to execute as long as i is not equal to 11. Inside the loop, i is incremented by 1 in each iteration, and count is also incremented by 1. Once i reaches 11, the condition i <> 11 becomes false, and the loop exits. Finally, the value of count, which represents the total number of iterations, is returned.
Additional Information:
- The condition in a WHILE loop is checked before executing the loop’s body. If the condition is false at the first check, the statements inside the loop will not execute at all.
- The
WENDkeyword is used to mark the end of the WHILE loop. - It’s important to ensure that the loop has a condition that will eventually become false; otherwise, the loop will create an infinite loop, which can freeze or crash the program.