Code 1 uses short circuit operator "&&". This evaluates the second part of the expression only if the first part is true. If the first part is false, the second part of the expression is ignored. In line 6, in the first part, t is true. Hence, the second part is evaluated and i is incremented to 1.In line 7, first part, f is false so, the second half of the expression is ignored. Hence, i = 1.
Code 2 uses logical "&" operator. Hence both expressions are evaluated and i = 3.
Hope you are clear with the answer.
suresh koutam
Ranch Hand
Joined: Dec 29, 2004
Posts: 30
posted
0
Ram...
this code has nothing to do with the short Circuit operators...they are asking the value of i....which you can evaluate just looking at the increment operators....its another way of confusing scjp aspirants....
Manfred Klug
Ranch Hand
Joined: Jun 04, 2007
Posts: 377
posted
0
Originally posted by suresh koutam: this code has nothing to do with the short Circuit operators...they are asking the value of i....which you can evaluate just looking at the increment operators....its another way of confusing scjp aspirants....
If this code has nothing to do with the short circuit operators, then I ask you for the detailed explanation why the outputs are different.
Bill Cruise
Ranch Hand
Joined: Jun 01, 2007
Posts: 148
posted
0
Suresh,
The output of the first example does depend on the short circuit operation.
This line of code
does not change the value of i due to the += operator being on the right-hand side of the short circuit && operator when the left-hand side is false.
suresh koutam
Ranch Hand
Joined: Dec 29, 2004
Posts: 30
posted
0
sorry guys..i was little excited seeing the answers...i agree with you guys.. thanks for correcting...