gpt4 book ai didi

goto - 使用带有开关的 goto vs while(wrongchoice)。

转载 作者:行者123 更新时间:2023-12-01 05:11:35 26 4
gpt4 key购买 nike

我想知道在这种情况下您不应该使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com