The three virtues of a programmer: Laziness, Impatience, and Hubris. – Larry Wall

While loop

From Unreal Wiki, The Unreal Engine Documentation Site
Jump to: navigation, search

The while loop is a basic loop statement in UnrealScript, similar to the for loop and the do...until loop.

Syntax[edit]

The while loop syntax is very similar to that of the if statement:

while (condition) {
  ...
}

The condition must be a bool type expression. The code between the parentheses is executed repeatedly until either the condition becomes False or a break statement is encountered.

Similar to the if statement, a while loop can be used to repeat a single statement instead of a block of code:

while (condition)
  statement;

This notation should be used with care because the statement could be the empty statement (a single semicolon), which would mean the loop only repeatedly evaluates its condition.

Quirks[edit]

In Unreal Engine 1 and 2 the compiler wasn't too strict when checking the until statement. You could not only omit it from a do loop, but even add it to a while loop, essentially creating a doubly-checked loop that has a condition at the start that must be true and a condition at the end that must be false for the loop to continue.

Like with the other loop constructs, you can specify a single statement instead of the code block after the condition:

while (condition)
  statement;

It is recommended to enclose even a single statement in curly braces to prevent potential misunderstandings and oversights. The statement may even be empty, thus reducing the code to the while keyword and the loop condition. If you accidentally put a semicolon after a while statement with a block, the compiler will complain. This won't happen for the single statement version, so you wouldn't notice the problem until you execute the code and run into an infinite loop.