views:

246

answers:

5

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.

+9  A: 

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
JaredPar
Unless your computer blows up or a BSOD pops up :) sorry, couldn't resist.
Mehrdad Afshari
s/executions/iterations/
Joel Coehoorn
+1  A: 

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

Galwegian
haha, nicely said. Though an error isn't nice too.
Dykam
+2  A: 

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.

Mitchel Sellers
+2  A: 

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
Matt Kellogg
Is there a setting for this instead? I have a ton of while loops and I do not want to add this to every one lol...
Cyclone
No. Again, it's not called an infinite loop for funsies!
Matt Kellogg
+2  A: 

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
Mark Bessey