The problem with asking this question is that you'll get so many subjective answers that simply state "I prefer this...". Instead of making such pointless statements, I'll try to answer this question with facts and references, rather than personal opinions.

Through experience, we can probably start by excluding the do-while alternatives (and the goto), as they are not commonly used. I can't recall ever seeing them in live production code, written by professionals.

The while(1), while(true) and for(;;) are the 3 different versions commonly existing in real code. They are of course completely equivalent and results in the same machine code.


for(;;)

  • This is the original, canonical example of an eternal loop. In the ancient C bible The C Programming Language by Kernighan and Ritchie, we can read that:

    K&R 2nd ed 3.5:

    for (;;) {
    ...
    }
    

    is an "infinite" loop, presumably to be broken by other means, such as a break or return. Whether to use while or for is largely a matter of personal preference.

    For a long while (but not forever), this book was regarded as canon and the very definition of the C language. Since K&R decided to show an example of for(;;), this would have been regarded as the most correct form at least up until the C standardization in 1990.

    However, K&R themselves already stated that it was a matter of preference.

    And today, K&R is a very questionable source to use as a canonical C reference. Not only is it outdated several times over (not addressing C99 nor C11), it also preaches programming practices that are often regarded as bad or blatantly dangerous in modern C programming.

    But despite K&R being a questionable source, this historical aspect seems to be the strongest argument in favour of the for(;;).

  • The argument against the for(;;) loop is that it is somewhat obscure and unreadable. To understand what the code does, you must know the following rule from the standard:

    ISO 9899:2011 6.8.5.3:

    for ( clause-1 ; expression-2 ; expression-3 ) statement
    

    /--/

    Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a nonzero constant.

    Based on this text from the standard, I think most will agree that it is not only obscure, it is subtle as well, since the 1st and 3rd part of the for loop are treated differently than the 2nd, when omitted.


while(1)

  • This is supposedly a more readable form than for(;;). However, it relies on another obscure, although well-known rule, namely that C treats all non-zero expressions as boolean logical true. Every C programmer is aware of that, so it is not likely a big issue.

  • There is one big, practical problem with this form, namely that compilers tend to give a warning for it: "condition is always true" or similar. That is a good warning, of a kind which you really don't want to disable, because it is useful for finding various bugs. For example a bug such as while(i = 1), when the programmer intended to write while(i == 1).

    Also, external static code analysers are likely to whine about "condition is always true".


while(true)

  • To make while(1) even more readable, some use while(true) instead. The consensus among programmers seem to be that this is the most readable form.

  • However, this form has the same problem as while(1), as described above: "condition is always true" warnings.

  • When it comes to C, this form has another disadvantage, namely that it uses the macro true from stdbool.h. So in order to make this compile, we need to include a header file, which may or may not be inconvenient. In C++ this isn't an issue, since bool exists as a primitive data type and true is a language keyword.

  • Yet another disadvantage of this form is that it uses the C99 bool type, which is only available on modern compilers and not backwards compatible. Again, this is only an issue in C and not in C++.


So which form to use? Neither seems perfect. It is, as K&R already said back in the dark ages, a matter of personal preference.

Personally, I always use for(;;) just to avoid the compiler/analyser warnings frequently generated by the other forms. But perhaps more importantly because of this:

If even a C beginner knows that for(;;) means an eternal loop, then who are you trying to make the code more readable for?

I guess that's what it all really boils down to. If you find yourself trying to make your source code readable for non-programmers, who don't even know the fundamental parts of the programming language, then you are only wasting time. They should not be reading your code.

And since everyone who should be reading your code already knows what for(;;) means, there is no point in making it further readable - it is already as readable as it gets.

Answer from Lundin on Stack Overflow
Top answer
1 of 12
128

The problem with asking this question is that you'll get so many subjective answers that simply state "I prefer this...". Instead of making such pointless statements, I'll try to answer this question with facts and references, rather than personal opinions.

Through experience, we can probably start by excluding the do-while alternatives (and the goto), as they are not commonly used. I can't recall ever seeing them in live production code, written by professionals.

The while(1), while(true) and for(;;) are the 3 different versions commonly existing in real code. They are of course completely equivalent and results in the same machine code.


for(;;)

  • This is the original, canonical example of an eternal loop. In the ancient C bible The C Programming Language by Kernighan and Ritchie, we can read that:

    K&R 2nd ed 3.5:

    for (;;) {
    ...
    }
    

    is an "infinite" loop, presumably to be broken by other means, such as a break or return. Whether to use while or for is largely a matter of personal preference.

    For a long while (but not forever), this book was regarded as canon and the very definition of the C language. Since K&R decided to show an example of for(;;), this would have been regarded as the most correct form at least up until the C standardization in 1990.

    However, K&R themselves already stated that it was a matter of preference.

    And today, K&R is a very questionable source to use as a canonical C reference. Not only is it outdated several times over (not addressing C99 nor C11), it also preaches programming practices that are often regarded as bad or blatantly dangerous in modern C programming.

    But despite K&R being a questionable source, this historical aspect seems to be the strongest argument in favour of the for(;;).

  • The argument against the for(;;) loop is that it is somewhat obscure and unreadable. To understand what the code does, you must know the following rule from the standard:

    ISO 9899:2011 6.8.5.3:

    for ( clause-1 ; expression-2 ; expression-3 ) statement
    

    /--/

    Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a nonzero constant.

    Based on this text from the standard, I think most will agree that it is not only obscure, it is subtle as well, since the 1st and 3rd part of the for loop are treated differently than the 2nd, when omitted.


while(1)

  • This is supposedly a more readable form than for(;;). However, it relies on another obscure, although well-known rule, namely that C treats all non-zero expressions as boolean logical true. Every C programmer is aware of that, so it is not likely a big issue.

  • There is one big, practical problem with this form, namely that compilers tend to give a warning for it: "condition is always true" or similar. That is a good warning, of a kind which you really don't want to disable, because it is useful for finding various bugs. For example a bug such as while(i = 1), when the programmer intended to write while(i == 1).

    Also, external static code analysers are likely to whine about "condition is always true".


while(true)

  • To make while(1) even more readable, some use while(true) instead. The consensus among programmers seem to be that this is the most readable form.

  • However, this form has the same problem as while(1), as described above: "condition is always true" warnings.

  • When it comes to C, this form has another disadvantage, namely that it uses the macro true from stdbool.h. So in order to make this compile, we need to include a header file, which may or may not be inconvenient. In C++ this isn't an issue, since bool exists as a primitive data type and true is a language keyword.

  • Yet another disadvantage of this form is that it uses the C99 bool type, which is only available on modern compilers and not backwards compatible. Again, this is only an issue in C and not in C++.


So which form to use? Neither seems perfect. It is, as K&R already said back in the dark ages, a matter of personal preference.

Personally, I always use for(;;) just to avoid the compiler/analyser warnings frequently generated by the other forms. But perhaps more importantly because of this:

If even a C beginner knows that for(;;) means an eternal loop, then who are you trying to make the code more readable for?

I guess that's what it all really boils down to. If you find yourself trying to make your source code readable for non-programmers, who don't even know the fundamental parts of the programming language, then you are only wasting time. They should not be reading your code.

And since everyone who should be reading your code already knows what for(;;) means, there is no point in making it further readable - it is already as readable as it gets.

2 of 12
38

It is very subjective. I write this:

while(true) {} //in C++

Because its intent is very much clear and it is also readable: you look at it and you know infinite loop is intended.

One might say for(;;) is also clear. But I would argue that because of its convoluted syntax, this option requires extra knowledge to reach the conclusion that it is an infinite loop, hence it is relatively less clear. I would even say there are more number of programmers who don't know what for(;;) does (even if they know usual for loop), but almost all programmers who knows while loop would immediately figure out what while(true) does.

To me, writing for(;;) to mean infinite loop, is like writing while() to mean infinite loop — while the former works, the latter does NOT. In the former case, empty condition turns out to be true implicitly, but in the latter case, it is an error! I personally didn't like it.

Now while(1) is also there in the competition. I would ask: why while(1)? Why not while(2), while(3) or while(0.1)? Well, whatever you write, you actually mean while(true) — if so, then why not write it instead?

In C (if I ever write), I would probably write this:

while(1) {} //in C

While while(2), while(3) and while(0.1) would equally make sense. But just to be conformant with other C programmers, I would write while(1), because lots of C programmers write this and I find no reason to deviate from the norm.

🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_infinite_loop.htm
C - Infinite Loop
In C language, an infinite loop (or, an endless loop) is a never-ending looping construct that executes a set of statements forever without terminating the loop. It has a true condition that enables a program to run continuously.
Discussions

[Beginner]Not sure why I can cause an infinite loop in this program?
So when you encounter a bug like this, a good first step is to keep cutting away code while checking to see whether the bug remains. You might end up with a simple program like this: #include int main(int argc, char **argv) { int number; while (1) { printf("Enter a number, or 0 to exit: "); scanf("%d", &number); if (number == 0) break; printf("You entered: %d\n", number); } return 0; } So what happens when you run this? Enter a number, or 0 to exit: 123 You entered: 123 Enter a number, or 0 to exit: 42 You entered: 42 Enter a number, or 0 to exit: abc Enter a number, or 0 to exit: You entered: 42 Enter a number, or 0 to exit: You entered: 42 Enter a number, or 0 to exit: You entered: 42 ... Aha, this also demonstrates the problem, and it's a lot easier to work with! So why does it do this? Think about what: scanf("%d', &number); does. It reads characters from standard input, but only if they are compatible with the format string. In this particular instance, the format string is "%d", which: reads and discards whitespace characters; then reads a decimal integer. But the character a is not a digit, nor it a plus or minus sign, so it can't possibly be part of an integer! This is the point where scanf stops reading. It leaves the a in the input stream for subsequent read operations.... but the subsequent read operations are still only looking for whitespace and integers. There's no way for this code to "get past" the a, let alone the b or the c. This here is fundamentally why scanf is a poor choice for reading unstructured data... and human input is just about the least structured data you can get. Humans can input anything! You would be better off reading a whole line as a string (say, with getline or fgets), then using strtol to convert that string to a number. Note that no matter what you do, you still need some kind of error handling. You weren't looking at the return values from your scanf calls, so you had no way to tell that it was giving up before reaching the end of the format string. But even if you replace scanf with other function calls, you still need to sure their return values. More on reddit.com
🌐 r/C_Programming
15
11
June 22, 2018
Is there something equivalent to C/Java's for(;;){} infinite for-loop syntax?
Since Kotlin has inline functions, you could create functions that would appear to be language constructs without incurring any overhead and could look like this: loop { println("Poof") } with something like this: inline fun loop(body: () -> Unit): Nothing { while (true) { body() } } More on reddit.com
🌐 r/Kotlin
23
11
July 31, 2022
New to C, (Hopefully) Easy Question about Infinite Loop Problem
The reason that your program is looping is that scanf is not consuming the input. You are telling it to find an integer with %d. If you type a number, scanf sees that it matches the "%d" and removes it from the input. However, if you type an "A", scanf sees that it does not match "%d" and stops reading the input without removing it. In other words, scanf stops scanning when the input does not match its format string. fgets is more what you are looking for. This will read a whole line of text, which can then be checked to see if it contains the password. fgets also will read the "enter" character as a newline ("\n"), so you will need to add that to your check, too. #include #include int main () { char password[64]; int accepted; printf ("Please Enter the Password:\n"); accepted = 0; while (!accepted) { fgets (password, sizeof(password), stdin); if (strcmp (password, "28\n") == 0) { printf ("Correct Password!\n"); accepted = 1; } else { printf ("Incorrect Password. Please try again.\n"); } } /* Other actions here */ return 0; } More on reddit.com
🌐 r/C_Programming
19
1
April 17, 2014
What is the fastest and best way to do an infinite loop inside the main loop?
Someone once said that for(;;) is the best, some swear by while(1) It's purely a stylistic decision. This ain't Python, and all practical compilers trivially transform these to the same thing. (I personally prefer the former.) More on reddit.com
🌐 r/C_Programming
26
6
April 10, 2022
🌐
Reddit
reddit.com › r/c_programming › what is the fastest and best way to do an infinite loop inside the main loop?
r/C_Programming on Reddit: What is the fastest and best way to do an infinite loop inside the main loop?
April 10, 2022 -

You know when you have options in your program and you have to to if-else argv argc stuff, and only THEN you actually go into the REAL infinite loop of your program.

How to do it? What is the best loop?

Someone once said that for(;;) is the best, some swear by while(1)

But then how do you (umm what's the word..) "exit gracefully" ? I've seen people having a variable like while (keep_looping), so they can get back to the main loop for cleanup and stuff and so they can return 0 or whatever other error code. But having a variable is obviously slower than just having 1 ? (what is even 1 in this case? a literal? either way it is definitely const and on the heap.)

Soo, what do you use? What should I use?

Top answer
1 of 10
16
Someone once said that for(;;) is the best, some swear by while(1) It's purely a stylistic decision. This ain't Python, and all practical compilers trivially transform these to the same thing. (I personally prefer the former.)
2 of 10
8
The "best" way to do this is likely an infinite loop and then a break statement wherever you detect the termination condition. I personally prefer while(1) because for loops should be used for count control and while loops should be used for logic control. Since a main program loop is almost always logic controlled it should be a while loop. With that in mind: I wouldn't fight someone on it. That's just how I chose one versus the other. But having a variable is obviously slower than just having 1 ? (what is even 1 in this case? a literal? either way it is definitely const and on the heap.) If the compiler implemented your code literally (without optimization) it could store the value 1 on the heap and then generate instructions to load that value into ACC (or your architecture's equivalent) and then jump if it is not 0. One optimization would be to just generate those instructions directly without loading from the heap: i.e. load ACC with 1 (the value 1 would be stored in the code section, not the data section) then jump if it isn't zero. The next optimization would be to remove the load entirely. The compiler can just unconditionally jump, which is obviously theoretically faster. In reality the branch predictor might handle this perfectly anyway.
🌐
Unstop
unstop.com › home › blog › infinite loop in c | types, causes, prevention (+examples)
Infinite Loop In C | Types, Causes, Prevention (+Examples)
October 17, 2024 - In C programming, infinite loops can be created using while loops, for loops, or do-while loops, where an invalid or missing exit condition allows the loop to continue indefinitely.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-loops
Loops in C - GeeksforGeeks
December 6, 2025 - A program is stuck in an Infinite loop when the condition is always true.
🌐
Quora
quora.com › Is-it-possible-to-create-an-infinite-loop-in-the-C-programming-language-without-using-the-goto-statement
Is it possible to create an infinite loop in the C programming language without using the goto statement? - Quora
Answer: Er, sure… [code]for (;;); [/code]We’ve got your infinite loop right here! [code]while (1); [/code]One with even fewer semicolons! A logically infinite loop that probably won’t be a “physical” one is [code]int foo(void) { return foo(); } [/code]These are sufficiently lame that most ...
Find elsewhere
🌐
Wikipedia
en.wikipedia.org › wiki › Infinite_loop
Infinite loop - Wikipedia
January 2, 2026 - An infinite loop is a sequence of instructions in a computer program which loops endlessly, either due to the loop having no terminating condition, having one that can never be met, or one that causes the loop to start over. In older operating systems with cooperative multitasking, infinite loops normally caused the entire system to become unresponsive.
🌐
ScholarHat
scholarhat.com › home
Infinite Loops in C: Types of Infinite Loops
July 31, 2025 - To learn more and enhance your skills, be sure to enroll in our C programming language online course free for comprehensive guidance. An infinite loop is a sequence of instructions that is executed endlessly without termination.
🌐
Medium
medium.com › @pkgmalinda › infinite-loops-in-c-understanding-avoiding-and-using-them-wisely-4af89ebe9573
Infinite Loops in C++: Understanding, Avoiding, and Using Them Wisely 🔄💻 | by Malinda Gamage | Medium
January 25, 2025 - An infinite loop occurs when the condition controlling the loop never becomes false, causing the loop body to execute repeatedly, without an exit. In C++, infinite loops can happen in for, while, and do-while loops.
🌐
Quora
quora.com › How-can-you-write-an-infinite-loop-in-C-using-a-for-statement
How to write an infinite loop in C using a 'for' statement - Quora
1.Initialization : Here you will initialize the loop variable i.e, You will assign an initial value to the loop variable. 2. Update: Here you will update the value of your loop variable every time the loop is executed to a specific value · 3.Termination : Here you will specify the condition which must be checked every time the loop is executed. ... Set of statements goes here. ... To stop an infinitely loop, First you must understand what is loop.
🌐
W3Schools
w3schools.com › c › c_for_loop.php
C For Loop
Statement 2 defines the condition for the loop to run: i < 5. If the condition is true, the loop will start over again, if it is false, the loop will end. Statement 3 increases a value each time the code block in the loop has been executed: i++
🌐
freeCodeCamp
freecodecamp.org › news › how-infinite-loops-work-in-c
How Infinite Loops Work in C++
August 11, 2025 - Infinite loops are caused when you forget to add an update condition inside the loop, which will terminate the loop in the future. The following program illustrates such a scenario: ... // Infinite loop caused due to missing update statement ...
🌐
OverIQ
overiq.com › c-programming-101 › the-infinite-loop-in-c › index.html
The Infinite Loop in C - C Programming Tutorial - OverIQ.com
The value of the whole expression (i=10) is 10, since a non-zero value is considered true, the condition is always true and the loop will go on to execute indefinitely. To fix the problem replace the expression (i=10) with (i==10). ... This loop is infinite because computers represent floating point numbers as approximate numbers, so 3.0 may be stored as 2.999999 or 3.00001.
🌐
C# Corner
c-sharpcorner.com › article › mastering-loops-in-c-sharp-a-complete-guide-with-best-practices-mistakes-and-when
Mastering Loops in C#: A Complete Guide With Best Practices, Mistakes, and When to Use What
November 24, 2025 - A while loop repeats as long as the condition is true. int i = 0; while (i < 5) { Console.WriteLine(i); i++; } ... ❌ Forgetting to modify the condition variable → infinite loop ❌ Using while when for loop is more readable
🌐
GeeksforGeeks
geeksforgeeks.org › c language › interesting-infinite-loop-using-characters-in-c
Interesting Infinite loop using characters in C - GeeksforGeeks
July 11, 2025 - In the above loop, the printf() statement will be executed 128 times. Now, look at the following program: ... Now can you guess how many times the printf() statement in the above "for" loop will be executed? Do you think this loop will also run 128 times? The answer is "NO".
🌐
OneCompiler
onecompiler.com › c › 3xa73j9q6
infinite loop - C - OneCompiler
Usually while is preferred when number of iterations are not known in advance. ... Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once. ... Array is a collection of similar data which is stored in ...
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › infinite-loop-in-cpp
Infinite Loop in C++ - GeeksforGeeks
July 23, 2025 - This is an infinite loop. This is an infinite loop. This is an infinite loop. This is an infinite loop. ........... In for loop, if we remove the initialization, comparison and updation condition, then it will result in infinite loop.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › c programming tutorial › infinite loop in c
Infinite Loop in C | Top 5 Examples of the Infinite Loop in C
April 1, 2023 - A loop that repeats indefinitely and does not terminate is called an infinite loop. An infinite loop also called as endless loop or indefinite loop. An infinite loop is most of the time create by the mistake, but it does not mean that infinite ...
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Programming Idioms
programming-idioms.org › idiom › 50 › make-an-infinite-loop › 493 › c
Make an infinite loop, in C
(do () (nil) (write-line "Control-C to quit this infinite loop") (sleep 1)) Doc · while true do -- Do something end · ::loop:: goto loop · repeat until false · for (;;); while (true) { // do something } While True do { nothing }; repeat until false; say "" while True; while (1) { do_stuff(); } ?- repeat, false. % repeat/0 is built-in, but could be defined like this: repeat.
🌐
TradingCode
tradingcode.net › csharp › loop › infinite-loop
C#'s infinite loops explained (causes & solutions) • TradingCode
But some loops don’t end and repeat the same code over and over again. Let’s see how we handle those loops. An infinite loop is a loop that keeps running indefinitely (Liberty & MacDonald, 2009; Wikipedia, 2019).