2

I am working on some code and I am trying to modify the end integer of a loop within said loop (as said in the title)

So it would look like this

dim x as integer

For i=0 to x
    if (some conditions) then
        x=x+1
    End if
Next 

However it doesn't seem to work, it looks like the loop is working with a static value of x and not a dynamic value.

Is there a way to update the end integer of the loop without using an exit for and starting the loop all over?

Thanks

  • 1
    In the documentation of [For...Next Statement (Visual Basic)](https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/for-next-statement), it says in the "Technical Implementation" section that the loop limits are evaluated only once, at the start. Also: [Why does a for loop behave differently when migrating VB.NET code to C#?](https://stackoverflow.com/a/52607681/1115360) – Andrew Morton Jun 28 '19 at 22:37

1 Answers1

5

You can use a While loop.

dim x as integer
Dim i as Integer = 0

While i <= x
    if (some conditions) then
        x=x+1
    End if
End While 
RobertBaron
  • 2,817
  • 1
  • 12
  • 19