Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Placing Infinite condition in c

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?

like image 733
Payal Patel Avatar asked Nov 30 '14 00:11

Payal Patel


People also ask

How will you create infinite loop in C?

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.

Are there infinite loops in C?

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.

How do you write an infinite loop?

We can create an infinite loop using while statement. If the condition of while loop is always True , we get an infinite loop.

How do you stop an infinite loop in C?

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.


1 Answers

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.

like image 200
Pranav Totla Avatar answered Sep 24 '22 22:09

Pranav Totla