Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

while(true) versus for(;;) [duplicate]

Possible Duplicates:
Is “for(;;)” faster than “while (TRUE)”? If not, why do people use it?
for ( ; ; ) or while ( true ) - Which is the Correct C# Infinite Loop?

Is there any appreciable difference between while(true) (or while(1)) and for(;;)? Would there be any reason to choose one over the other?

like image 559
kylan Avatar asked Sep 27 '10 20:09

kylan


People also ask

What is the difference between while and while true?

The while loop in python runs until the "while" condition is satisfied. The "while true" loop in python runs without any conditions until the break statement executes inside the loop.

What is the difference between for and while 1?

no functional difference at all, just a matter of taste.

Why do we use while true?

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.

Is while 1 the same as while true?

Why does while distinguish the two? 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.


2 Answers

With optimizations enabled, they will compile identically.

You should use whichever one you find more readable.

like image 165
SLaks Avatar answered Sep 25 '22 08:09

SLaks


No. I think for(;;) looks nicer. But they're the same.

Also see Is "for(;;)" faster than "while (TRUE)"? If not, why do people use it?

like image 21
Charles Salvia Avatar answered Sep 25 '22 08:09

Charles Salvia