Cyclone
Cyclone

Reputation: 18295

What is the maximum number of executions in a while loop in VB.net that it will allow?

What is the maximum number of executions in a while loop in VB.net that it will allow? Meaning, it is checking for a variable to equal some value, but that value never comes? How many times will it execute the code before it quits? Is there some way to set the maximum number of executions without terminating it programmatically?

Thanks for the help.

Upvotes: 0

Views: 2884

Answers (5)

Mark Bessey
Mark Bessey

Reputation: 19782

If you want to loop a certain number of times until some event occurs, the usual solution is to combine the test for the condition and the loop count in the while test.

while (not done) and loops < 1000
  loops = loops + 1
  If () then done=true
end while

Upvotes: 2

Matt Kellogg
Matt Kellogg

Reputation: 1252

It's not called an infinite loop for no reason.

You could do:

Dim backupExit as Integer

While Not myExitCondition AndAlso backupExit < someValue
    ''//do stuff
    backupExit += 1
End While

Upvotes: 2

JaredPar
JaredPar

Reputation: 754853

The While loop in VB.Net has no inherent limitation on number of iterations. It will execute exactly as many times as your code says it should.

For example, the following loop won't ever exit

While True
  Console.WriteLine("hello")
End While

Upvotes: 9

Mitchel Sellers
Mitchel Sellers

Reputation: 63126

The situation you are discussing is an endless loop. It is called that because there is nothing that will stop the loop from executing.

You would need to code in a loop counter, or switch the type of loop to have it exit early.

Upvotes: 2

Galwegian
Galwegian

Reputation: 42247

If there were a limit, we may not have to worry about the infinite loop ;-)

Upvotes: 1

Related Questions