[英]Why are while and do-while loops preferred over for loops when looping infinitely?
I'm new to programming and am in my first year of computer science and am a bit confused about loops. 我是编程的新手,在计算机科学的第一年,对循环有点困惑。 When it comes to infinite loops, why are while and do-while loops preferred over for loops?
当涉及无限循环时,为什么while和do-while循环比for循环更受青睐? I created a simple infinite for loop and it's just as easy as creating a while loop.
我创建了一个简单的for循环,就像创建while循环一样简单。 Is one type of loop faster than the other?
一种循环比另一种更快吗?
因为它可能比for(;;)
更容易编写(和读取) while(true)
的意图(或者读其意图)?
A "for()" loop is pretty much equivalent to initializing an index, declaring a while() condition, and incrementing/decrementing the loop. “ for()”循环几乎等效于初始化索引,声明while()条件以及递增/递减循环。
There is absolutely no performance difference. 绝对没有性能差异。
"while (true)" is generally preferred over "for (;;)" except for people like me who've read and revere the original K & R "White Book" - "The C Programming Language" :) 通常,“ while(true)”比“ for(;;)”更可取,除了像我这样的人,他们已经阅读并喜欢原始的K&R“ White Book”-“ The C Programming Language” :)
Neither one is faster than the other. 哪一个都不比另一个快。 It's just that:
就是这样:
while (true)
{
}
looks more intuitive and human-readable than: 看起来比:
for (; ; )
{
}
while(true)
比for(;;)
更容易阅读-您不需要弄清楚它的含义-它几乎用英语拼写出来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.