The Infinite Loop in C
A loop that repeats indefinitely and never terminates is called an Infinite loop.
Loops that go on executing forever and never terminates because of lack of an exit condition are called infinite loops. Sometimes we create these loops by mistake, while sometimes we deliberately create them in our program. Let’s take some examples and see what kind of mistakes can lead to infinite loop.
This loop is an infinite loop. Here is why ? According to condition given, the loop will execute until . Initially, the value of is and after each iteration, its value is decremented in the update expression (), so the value of will never be greater than . Hence the condition will always be true. To make it finite we should use instead of .
Here we are not updating the value of . So after each iteration value of remains same. As a result, the condition will always be true. For the loop to work correctly add , just after the statement.
Always Remember, it is easy to forget update expression in while and do while loop, than in for loop.
Another common mistake that lead to infinite loop is to use assignment operator () where equality operator is needed ().
What we actually wanted is that loop should execute until is equal to . The value of the whole expression is , 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 use instead of .
This loop is infinite because computers represent floating point numbers as approximate numbers, so may be stored as or . So the condition never becomes false. To fix this problem write the condition as .
This loop will produce no output and will go on executing indefinitely. Take a closer look and notice the semicolon() at the end of while condition. We know semicolon() after the condition is a null statement. So the loop is treated as following:
As you can see there is no update expression inside the while loop, that's why it will go on executing indefinitely.
There are two different operators in play, here. An assignment operator, and a comparison operator, . Clearly they have widely different purposes.
is an expression, whereas,
is a statement. We are assigning the expression, to the variable, . This is an explicit action, not a conditional. Any such statement cannot be used as a conditional expression in any comparison based statement. We should never write an assignment in the or conditional. You have already experienced the consequences.
In the above, when we set we have defined a boolean.
The reason the conditional works with (evaluates to a boolean) and with (already a boolean) is because both are booleans. The difference is that one is an expression and the other a value, but they equate to the same thing.
Something that can be evaluated to a boolean 'true' is called truthy, and something that can be evaluated to a boolean 'false' is called falsy. The optimum word here is, evaluate.
A conditional expression always evaluates to a boolean, no matter what the expression or how complex or convoluted. ("My dog is brown") => true. What? How's that?
We don't know if my dog is brown without looking at, so that's not what is necessarily true. What is true, though, is that this is not an empty string, so when evaluated as a boolean returns 'true'. ("") => false.
I don't wish to complicate this answer any further but would fervently recommend that you read up on,