简体   繁体   中英

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? I created a simple infinite for loop and it's just as easy as creating a while loop. 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.

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" :)

Neither one is faster than the other. It's just that:

while (true)
{
}

looks more intuitive and human-readable than:

for (; ; )
{
}

while(true)for(;;)更容易阅读-您不需要弄清楚它的含义-它几乎用英语拼写出来。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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