For loop

From Unreal Wiki, The Unreal Engine Documentation Site
(Redirected from For)
Jump to navigation Jump to search

The for loop is one of four different loop statements in UnrealScript. The others are the Do/Until, While and ForEach loops.


The general syntax of the for loop is as follows:

for (initstatement; loopcondition; updatestatement)

Instead of a single statement, it is more common to follow up with a block of code, even if it only contains a single statement as well:

for (initstatement; loopcondition; updatestatement) {

The initstatement must be exactly one assignment or function call and is executed right before the loop is entered for the first time. The loopcondition must be a bool type expression and is evaluated before each (including the first) loop iteration. If the condition evaluates to False, the loop ends and code execution continues at the first statement after the for loop. The updatestatement must be exactly one assignment or function call and is executed at the end of each loop iteration right before the loop condition is reevaluated. All three parts in the parentheses must be specified. Unlike other languages that allow you to omit these expressions, the UnrealScript compiler will complain if any of them is missing. Also unlike other languages the UnrealScript compiler will complain about comma-separated assignments as initialization or update statements.

Note: The compiler also allows the empty statement (a lonely semicolon) as statement after the for statement. This is basically the same as specifying an empty block of code. Any other statements following the semicolon are not part of the loop! Be wary of this when using the single-statement form of the for loop.