Home > Software design >  Vbscript, how to write a For loop with two variables
Vbscript, how to write a For loop with two variables

Time:03-31

I am use Vb to write for loop, i=1 to 3 j=1 to 3

how to make the i j result become 1 1,2 2, 3 3.

I know for C#, it can be for(i=0,j=0;i<10,j<10;i ,j ) just don't know how to write in VB

CodePudding user response:

Unfortunately, as of March 2022, Microsoft seems to have finally pulled their 25 year-old VBScript language documentation from their documentation website, however third-party content-mirrors exist with the documentation for VBScript's For Next statement

TL;DR: You can't. VBScript's For Next statement only supports updating a single variable inside.

You can still declare and manually increment your own variables in the loop though, e.g.

Using a single variable:

Option Explicit

Dim i
For i = 0 To 5 Step 1

    Call DoStuff

Next

Using multiple variables requires separate Dim declarations and incrementing them inside the For loop body:

Option Explicit

Dim i, j, k
Let j = 0
Let k = 0

For i = 0 To 5 Step 1

    Call DoStuff

    Let j = j   1
    Let k = k   1
Next

CodePudding user response:

Dim result As Integer
    For i = 1 To 3
        For j = i To i
            result = i   j
            Console.WriteLine(i.ToString   " "   j.ToString   "="   result.ToString)
        Next
    Next
  • Related