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....

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.