Monday, May 24, 2010

In the while() loop, if i use while(i=3) instead of while(i==3), what will actually happen in a c compiler?

will it generate a error or create a infinite loop?

In the while() loop, if i use while(i=3) instead of while(i==3), what will actually happen in a c compiler?
That will cause an infinite loop. Because you keep initiating i to be equal to 3. Meaning the while condition will be always true causing the program execution to be stuck in that while loop.
Reply:It will assign 3 to i and then test 3 against zero. Finding that 3 is not zero, it will stay in the loop. It will do this each time through the loop and never exit.





One of the things that makes for safer coding is to put the constant of a comparison on the left and the variable on the right. Instead of testing (i == 3), test (3 == i). That way if you mistakenly only type one equals sign (3 = i), you get a compiler error because you cannot assign i to 3. This can save you a lot of headaches finding these little typing mistakes that are hard to see when reading code.
Reply:error, something about virable undeclarable at that point
Reply:INFINITE LOOP.


While(i=3)


it takes infinite time to run.your dos prompt full of same statements until you press CONTROL+BREAK


While(i==3)


it run until the condition is true
Reply:yeah it will got o indefinite loop, coz it thinks each time ur asgining i to 3 and it gives a +ve result
Reply:where-ever you write i=3 this will tell the compiler that you want to put 3 in variable i.


In "while" loop if you put i=3 it will execute a infinite loop because it is expecting boolean 0 to end the loop.


No comments:

Post a Comment