作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想知道在这种情况下您不应该使用 goto 而不是使用 while(variable) 是否有原因:
:start
result=askUser("do you want 1,2,or 3?")
switch(result){
case 1: display("you chose 1")
case 2: display("you chose 2")
case 3: display("you chose 3")
default: display("choice not availaible")
goto :start
}
对比
boolean wrongchoice=false
do
result=askUser("do you want 1,2,or 3?")
switch(result){
case 1: display("you chose 1")
case 2: display("you chose 2")
case 3: display("you chose 3")
default: display("choice not availaible")
wrongchoice=true
}
while(wrongchoice)
在我看来,goto 在小型 µc 上会更好,因为您不必设置变量并为其使用内存(当您只有 256 时,一点就是一点)
对于今天的计算机来说,贪图几个位是徒劳的,那么使用 while 是不是就更清楚了呢?
最佳答案
for today's computers, being greedy for a few bits is futile though, so is using the while much more clear then?
没错。在绝大多数情况下,可读性和可维护性比性能更重要。
我遇到过 goto
即使使用 -O3 优化也比 while 循环更快的情况,但这是在分子动力学模拟器的 O(n^2) 算法的最内层循环中.除非密集的用例(需要在代码的生命周期内进行更密集的维护!),请使用 while
因为它更具可读性并且不太可能做奇怪的事情。
关于goto - 使用带有开关的 goto vs while(wrongchoice)。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41038208/
我想知道在这种情况下您不应该使用 goto 而不是使用 while(variable) 是否有原因: :start result=askUser("do you want 1,2,or 3?") sw
我是一名优秀的程序员,十分优秀!