for Statement

Iteratively executes a series of statements, based on the evaluation of the conditional expression.

[Attribute] for ( Initializer; Conditional; Iterator ) { Statement Block; }

Parameters

Attribute

An optional parameter that controls how the statement is compiled. When no attribute is specified the compiler will first attempt to emit a rolled version of the loop, and if that fails, or if some operations would be easier if the loop was unrolled, will fall back to an unrolled version of the loop.

Attribute Description
unroll(x) Unroll the loop until it stops executing. Can optionally specify the maximum number of times the loop is to execute. Not compatible with the [loop] attribute.
loop Generate code that uses flow control to execute each iteration of the loop. Not compatible with the [unroll] attribute.
fastopt Reduces the compile time but produces less aggressive optimizations. If you use this attribute, the compiler will not unroll loops.
This attribute affects only shader model targets that support break instructions. This attribute is available in shader model vs_2_x and shader model 3 and later. It is particularly useful in shader model 4 and later when the compiler compiles loops. The compiler simulates loops by default to evaluate whether it can unroll them. If you do not want the compiler to unroll loops, use this attribute to reduce compile time.
allow_uav_condition Allows a compute shader loop termination condition to be based off of a UAV read. The loop must not contain synchronization intrinsics.

Initializer

The initial value of the loop counter.

Conditional

A conditional expression. If the conditional expression evaluates to true, the statement block is executed. The loop ends when the expression evaluates to false.

Iterator

Update the value of the loop counter.

Statement Block

One or more HLSL statements.

Remarks

The [unroll] and [loop] attributes are mutually exclusive and will generate compiler errors when both are specified.

The [fastopt] and [allow_uav_condition] attributes are ignored if [unroll] is specified.

See also

Flow Control