Wikis / Unreal Wiki / While loop

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

Syntax

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

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.

Page Categories

Page Information

2022-11-18T07:02:02.960955Z 2010-08-25T20:59:22Z Wormbo added quirks and recommendations https://wiki.beyondunreal.com/While loop Attribution-NonCommercial-ShareAlike 3.0