DevPinoy.org
A Filipino Developers Community
ATTENTION:Take our Code Challenge for the month of October 2008 and win some great prize! Learn more about it here!

Assignment in a WHILE condition

Before, I used to think that in C, the assignment operator always evaluate to true.  It is because of this context:
[code language="C#"]
while (intLeft == intRight)
{
    //do whatever
}
[/code]

If you mistyped the comparison operator (==) as a single equal sign (=) for assignment (of course it will compile and run since we were using Turbo C), the initial test of the while condition succeeds since I always test the code with intRight’s value as non-zero, and often leads to an infinite loop.

I just found out earlier that the assignment operator always evaluate to the value of the LHS which takes the value of RHS after the assignment.  And since it is used in a while condition, the integer value is treated as boolean (zero or non-zero), talk about strong type system of .NET.

Posted Jan 17 2005, 05:34 PM by jokiz
Filed under: , , ,

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:

Copyright DevPinoy 2005-2008
Powered by Community Server (Commercial Edition), by Telligent Systems