Which is faster, "for(;)" or "while(TRUE)?

Asked 1 years ago, Updated 1 years ago, 126 views

[Which is faster, (1) or (2)?I thought of this while looking at

for (;;) {
    //Something to be done repeatedly
}

I've seen a lot of codes like this, but I haven't seen them write in while(true).

I think while(true) is more readable if you're going to use it. Why is that?

Are you writing a for statement because for(;;) is faster than while(true)?

c c++ optimization readability infinite-loop

2022-09-21 16:52

1 Answers

Both have the same assembly code, so their performance is the same.

The reason why for(;) is usually used more is

This is because the compiler warns you that "loop condition is constant" in while(true)

Because for(;) is "no condition" and is closer to infinite loop. Doesn't (while(true) seem to have the condition true? ;)

;);)


2022-09-21 16:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.