<< Prev        Next >>

ID : 3747

For...Next

Function

To repeat a set of statements up to the specified number of times.

Syntax

For counter = initial value To final value [Step increment]
	'Statements
Next[ counter]

Guaranteed Entry

Counter
Designate a numeric type variable to be used as a counter.
Initial value
Designate a value to assign as an initial value of the variable designated in counter.
Final value
Designate a final value of counter.
Increment
Designate a value to add to counter every time a set of statements is executed. This is an optional value. This should be "1" if this is omitted.

Description

A set of statements is repeated up to the specified number of times.

Initial value is assigned to the variable designated in counter to judge the execution condition. If the execution condition is True, the designated set of statements is executed. A value designated in increment is add to the counter variable every time a set of statements is executed.

Statement execution condition

Judgment of the execution condition of the designated statements is conducted before execution of the designated statements. The termination condition varies depending on the value in increment.

Value of increment Execution condition
Positive number or 0 Counter <= Final value
Negative number Counter >= Final value

To compulsively exit this loop, execute "Exit For" of the Exit statement and then the next line of Next is called.

Related Terms

Do...Loop, Exit

Attention

A loop becomes infinite when "0" is designated in increment.

To nest a For block (nesting structure), do not designate the same variable in counter. Otherwise, behaviors may get complex, leading to an unexpected infinite loop.

The final value and increment are passed by value and fixed once the For statement is executed. Any change in the variable designated in final value during execution of the set of statements will not be applied to final value.

Example

'!TITLE "Repeated Execution of Instruction between For and Next"
' Addition of value between 1 and 10
Sub Sample_ForNext

  Dim aaa As Integer
  Dim bbb As Integer

  bbb = 0

  ' Addition processed until aaa becomes larger than 10
  For aaa = 0 To 10 Step 1

    bbb = bbb + aaa

  Next

  ' Display "55" on the message output window
  PrintDbg bbb

End Sub

ID : 3747

<< Prev        Next >>