Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
As the operators work on Right-to-left, first the check on (1==2) is happening.
The result of the comparison is assigned to a boolean variable.
This is compatible and without compile-time error because the result of comparison (remember! == operator and NOT = operator) would always be of type boolean (true or false).
Since the result of (1==2) is false (obviously! ), the same 'false' is assigned to the boolean variable 'a' (here you got to be careful, because you had used = operator). The result of an assignment will be the value of the expression.
So, the value 'false' is assigned to 'a' and is checked in if loop thus making the 'else' statement eligible for execution. So, you get "banana" instead of "apples" contradicting to your guess!
Originally posted by Divya Gehlot: But in If condition we will check the condition if its true then execute if otherwise exceute else. here we instead of comparing we have assigned the value then why its going to else part.
It is going to the "else" part because the condition is false. The condition in this case is the value of the assignment, which is false.