For...Next Statement [Runtime]/text/sbasic/shared/03090202.xhpFor statementTo statementStep statementNext statementFor...Next Statement [Runtime]Repeats the statements between the For...Next block a specified number of times.Syntax:For counter=start To end [Step step]statement block[Exit For]statement blockNext [counter]Variables:Counter: Loop counter initially assigned the value to the right of the equal sign (start). Only numeric variables are valid. The loop counter increases or decreases according to the variable Step until End is passed.Start: Numeric variable that defines the initial value at the beginning of the loop.End: Numeric variable that defines the final value at the end of the loop.Step: Sets the value by which to increase or decrease the loop counter. If Step is not specified, the loop counter is incremented by 1. In this case, End must be greater than Start. If you want to decrease Counter, End must be less than Start, and Step must be assigned a negative value.The For...Next loop repeats all of the statements in the loop for the number of times that is specified by the parameters.As the counter variable is decreased, $[officename] Basic checks if the end value has been reached. As soon as the counter passes the end value, the loop automatically ends.It is possible to nest For...Next statements. If you do not specify a variable following the Next statement, Next automatically refers to the most recent For statement.If you specify an increment of 0, the statements between For and Next are repeated continuously.When counting down the counter variable, $[officename] Basic checks for overflow or underflow. The loop ends when Counter exceeds End (positive Step value) or is less than End (negative Step value).Use the Exit For statement to exit the loop unconditionally. This statement must be within a For...Next loop. Use the If...Then statement to test the exit condition as follows:For...statementsIf condition = True Then Exit ForstatementsNextNote: In nested For...Next loops, if you exit a loop unconditionally with Exit For, only one loop is exited.ExampleThe following example uses two nested loops to sort a string array with 10 elements ( sEntry() ), that are first filled with various contents:Sub ExampleSortDim sEntry(9) As StringDim iCount As IntegerDim iCount2 As IntegerDim sTemp As StringsEntry(0) = "Jerry"sEntry(1) = "Patty"sEntry(2) = "Kurt"sEntry(3) = "Thomas"sEntry(4) = "Michael"sEntry(5) = "David"sEntry(6) = "Cathy"sEntry(7) = "Susie"sEntry(8) = "Edward"sEntry(9) = "Christine"For iCount = 0 To 9For iCount2 = iCount + 1 To 9If sEntry(iCount) > sEntry(iCount2) ThensTemp = sEntry(iCount)sEntry(iCount) = sEntry(iCount2)sEntry(iCount2) = sTempEnd IfNext iCount2Next iCountFor iCount = 0 To 9Print sEntry(iCount)Next iCountEnd Sub