I am running a C program with an infinite for
loop:
for(;;)
{
//Statement
}
Why is it running an infinite number times, even though we have not specified the loop's initialization, condition and incrementation?
What do the "blank" values mean?
while loop represents the infinite condition as we provide the '1' value inside the loop condition. As we already know that non-zero integer represents the true condition, so this loop will run infinite times. We can also use the goto statement to define the infinite loop.
Introduction to Infinite Loop in C. 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.
We can create an infinite loop using while statement. If the condition of while loop is always True , we get an infinite loop.
There is no way to stop an infinite loop. However, you can add a condition inside of the loop that causes it to break, or you can call the exit() function inside of the loop, which will terminate your program. Highly active question.
Here's the basic syntax of a for loop.
for(clause-1; expression-2; expression-3) statement;
According to The C Programming Language by K&R, both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a non-zero constant. And as we know, any non-zero value means "true" in C.
P.S.: Though the K&R book is quite outdated, it's considered as the Bible of C by many.
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