简体   繁体   English

在VB.net中的while循环中允许的最大执行次数是多少?

[英]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? 在VB.net中的while循环中允许的最大执行次数是多少? 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. 谢谢您的帮助。

The While loop in VB.Net has no inherent limitation on number of iterations. VB.Net中的While循环对迭代次数没有固有的限制。 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

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. 您可能需要在循环计数器中编写代码,或切换循环类型以使其尽早退出。

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

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

如果有限制,我们可能不必担心无限循环;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM