locked
What does Step do in a for Loop

    Question

  • For j As Int32 obj.Value.Count - 1 TO 0 Step -1.....What does the step do??? Does the step make the loop read backwards or something?????
    Monday, February 05, 2007 5:04 PM

Answers

  • It is what the variable gets incremented by on each step.

    For i As Integer = 0 To 10 Step 2
        Console.WriteLine(i)
    Next

    Would print out the even numbers from 0 to 10.

    For i As Integer = 10 To 0 Step -2
        Console.WriteLine(i)
    Next

    Would print out the even numbers from 10 to 0 (10-2 = 8, 8-2 = 6, etc.).
    Monday, February 05, 2007 5:10 PM