CodeIterationStatement Class


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Represents a for statement, or a loop through a block of statements, using a test expression as a condition for continuing to loop.

Namespace:   System.CodeDom
Assembly:  System (in System.dll)


Public Class CodeIterationStatement
	Inherits CodeStatement


Initializes a new instance of the CodeIterationStatement class.

System_CAPS_pubmethodCodeIterationStatement(CodeStatement, CodeExpression, CodeStatement, CodeStatement())

Initializes a new instance of the CodeIterationStatement class using the specified parameters.


Gets a CodeDirectiveCollection object that contains end directives.(Inherited from CodeStatement.)


Gets or sets the statement that is called after each loop cycle.


Gets or sets the loop initialization statement.


Gets or sets the line on which the code statement occurs. (Inherited from CodeStatement.)


Gets a CodeDirectiveCollection object that contains start directives.(Inherited from CodeStatement.)


Gets the collection of statements to be executed within the loop.


Gets or sets the expression to test as the condition that continues the loop.


Gets the user-definable data for the current object.(Inherited from CodeObject.)


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)


Creates a shallow copy of the current Object.(Inherited from Object.)


Returns a string that represents the current object.(Inherited from Object.)

A CodeIterationStatement can represent a for loop or while loop.

The InitStatement property specifies the statement to execute before the first loop iteration. The TestExpression property specifies the loop continuation expression, which must evaluate to true at the end of each loop iteration for another iteration to begin. The IncrementStatement property specifies the statement to execute at the end of each loop iteration. The Statements property specifies the collection of statements to execute within the loop.

This example demonstrates using a CodeIterationStatement to represent a for loop.

 ' Declares and initializes an integer variable named testInt.
 Dim testInt As New CodeVariableDeclarationStatement(GetType(Integer), "testInt", New CodePrimitiveExpression(0))

 ' Creates a for loop that sets testInt to 0 and continues incrementing testInt by 1 each loop until testInt is not less than 10.
    ' initStatement parameter for pre-loop initialization.
    ' testExpression parameter indicates the epxression to test for continuation condition.
    ' incrementStatement parameter indicates statement to execute after each iteration.
    ' statements parameter contains the statements to execute during each interation of the loop.
    ' Each loop iteration the value of the integer is output using the Console.WriteLine method.

 Dim forLoop As New CodeIterationStatement( _
    New CodeAssignStatement(New CodeVariableReferenceExpression("testInt"), New CodePrimitiveExpression(1)), _
    New CodeBinaryOperatorExpression(New CodeVariableReferenceExpression("testInt"), _ 
        CodeBinaryOperatorType.LessThan, New CodePrimitiveExpression(10)), _
    New CodeAssignStatement(New CodeVariableReferenceExpression("testInt"), _
    New CodeBinaryOperatorExpression(New CodeVariableReferenceExpression("testInt"), _
        CodeBinaryOperatorType.Add, New CodePrimitiveExpression(1))), _
    New CodeStatement() {New CodeExpressionStatement( _
        New CodeMethodInvokeExpression(New CodeMethodReferenceExpression(New CodeTypeReferenceExpression("Console"), "WriteLine"), _ 
                New CodeMethodInvokeExpression(New CodeVariableReferenceExpression("testInt"), "ToString")))})

' A Visual Basic code generator produces the following source code for the preceeding example code:

'     Dim testInt As Integer = 0
'     testInt = 1
'     Do While (testInt < 10)
'         Console.WriteLine(testInt.ToString)
'         testInt = (testInt + 1)

.NET Framework
Available since 1.1

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top