Sahil
Sahil

Reputation: 115

Why the output is infinite?

int i;
while(i=10)
{
    printf("%d\n",i);
    i=i+1;
}

There is a program in which while(i=10) means while(1)[usually] means the while loop is true.So it should print 10 only once and increment the value of i by 1 i.e i becomes 11.but the output is infinite printing ‘10’ repeatedly.Why is this so? My Platform is : 64 bit Windows 7 and working on 32 bit Dev C++ compiler.

Upvotes: 0

Views: 1985

Answers (6)

darioses
darioses

Reputation: 81

I think you want to use a double equal (comparison) instead of the single equal (assignment) in the while condition. You should also initialize the variable i to 10 (otherwise the while block will never be executed).

int i=10;
while (i==10)
{
    printf("%d\n",i);
    i=i+1;
}

Upvotes: 8

Omkant
Omkant

Reputation: 9234

See in while loop ,

while(i=10) // assigns the value 10 to the i which is always true (becuase any non-zero value is true in such a case).

What you have used is assignment operator = so the value returned by this assignment is the value of rvalue which is 10 and 10 will always be true.

That's why infinite loop.

You should use while(i == 10) or while(i < = 10) or any comparison operator inside while loop.

Comparison operator results to either 1(true) or 0(false).And when in while(cond) cond is false loop terminates.

Upvotes: 5

BoBTFish
BoBTFish

Reputation: 19767

while(i=10) sets i to 10, and then checks it, i.e. converts it to true or false. Only 0 would be false, so this becomes true every time, and hence loops forever.

I guess you want while(i==10). Double = means "check equality". Single = means "set the value".

Edit: In fact I'm not really sure what you want. If you aren't initialising i before the loop, then perhaps you did mean to set it in the condition. In which case, why are you surprised it gets set to 10 every time?

Edit again: Ok, so by your comment, you do mean to set it in the condition. So you need to understand how that condition is working. What you wrote is effectively this:

while ( (bool)(i=10) )

This sets i to 10, then converts 10 to a bool, which is always true. Then in the loop, i gets incremented to 11. Then when it comes back to the top of the loop, it immediately sets i back to 10. So you are effectively doing:

set i to 10; <-----\
print i (=> 10);    |
set i to 11;        |
go back to the top; /

Upvotes: 2

Phil H
Phil H

Reputation: 20151

I think the key thing you are missing is that the condition statement is executed every iteration of the loop. So i=10 is performed (and the resulting reference tested against zero) for every iteration of the loop, not just the first one.

Your code is equivalent to this:

int i;
int j = (i=10);
while(j)
{
    printf("%d\n",i);
    i=i+1;
    j = (i=10);
}

Where j = (i=10); is equivalent to i = 10; j = i;.

So the steps become:

  1. assign 10 to i, and assign the value in the resulting reference (i) to j (assignments return references or pointers by convention).
  2. if the value is true (which it always is), execute the loop block
    1. call printf
    2. increment i
    3. assign 10 to i, and assign the value in the resulting reference (i) to j.

A while loop testing an assignment can only fail/stop looping if the value returned from the assignment is zero.

In practise, it is bad style to write loops like this, because they look too similar to the very common while(i==10) equality check. Sometimes the return value of an assignment is tested when objects are being used, like while(nextObjPtr = collection.getNextObj()), because that will fail as soon as getNextObj returns a null pointer. Even that, however, would be more clearly written explicitly:

while((nextObjPtr = collection.getNextObj()) != nullptr)

Or pre-C++11:

while((nextObjPtr = collection.getNextObj()) != NULL)

Upvotes: 1

Mudassir Hasan
Mudassir Hasan

Reputation: 28771

In your while condition section , you are assigning value to i rather than doing comparison

 while(i=10)

Errors of such type are hard to track as program compiles successfully . To avoid such errors do comparison in reverse order

while(10==i) or if(10==x) etc..

in case you mistakenly use while(10=i) , the compiler will throw error something like

The left-hand side of an assignment must be a variable, property or indexer

saving precious time debugging later.

Upvotes: 2

andrea.marangoni
andrea.marangoni

Reputation: 1499

  while(i=10)

the condition is wrong! Actually it is not a condition!

you should write

while(i<10)

Upvotes: 0

Related Questions