If my understanding is correct, they do exactly the same thing. Why would anyone use for the "for" variant? Is it just taste?
Edit: I suppose I was also thinking of for (;;).
While loop is used to execute a block of code repeatedly until given boolean condition evaluated to False. If we write while True then the loop will run forever.
It distinguishes 1 and True in exactly the same way that the + example does. It's emitting a LOAD_GLOBAL (True) for each True , and there's nothing the optimizer can do with a global. So, while distinguishes 1 and True for the exact same reason that + does.
Master C and Embedded C Programming- Learn as you go The while is a loop of C or C++. Using this loop we can check one condition, and the statements inside the loop will be executed while the condition is true. The while(1) or while(any non-zero value) is used for infinite loop. There is no condition for while.
while(condition == true) will be true while condition is true . You can make that false by setting condition = false .
for (;;)
is often used to prevent a compiler warning:
while(1)
or
while(true)
usually throws a compiler warning about a conditional expression being constant (at least at the highest warning level).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With