# short circuit operators

ketki kalkar

Ranch Hand

Posts: 36

posted 8 years ago

- 0

1.public static ShortCkt {

public static void main(String args[]) {

int i = 0;

boolean t = true;

boolean f = false, b;

b = (t && ((i++) == 0));

b = (f && ((i+=2) > 0));

System.out.println(i);

}

}

answer is 1.

2.public static ShortCkt {

public static void main(String args[]) {

int i = 0;

boolean t = true;

boolean f = false, b;

b = (t & ((i++) == 0));

b = (f & ((i+=2) > 0));

System.out.println(i);

}

}

0

1

2

3

answer is 3.

Please explain?

0

1

2

3

public static void main(String args[]) {

int i = 0;

boolean t = true;

boolean f = false, b;

b = (t && ((i++) == 0));

b = (f && ((i+=2) > 0));

System.out.println(i);

}

}

answer is 1.

2.public static ShortCkt {

public static void main(String args[]) {

int i = 0;

boolean t = true;

boolean f = false, b;

b = (t & ((i++) == 0));

b = (f & ((i+=2) > 0));

System.out.println(i);

}

}

0

1

2

3

answer is 3.

Please explain?

0

1

2

3

suresh koutam

Ranch Hand

Posts: 30

ram shah

Greenhorn

Posts: 28

posted 8 years ago

- 0

1.1.public static ShortCkt {

2.public static void main(String args[]) {

3.int i = 0;

4.boolean t = true;

5.boolean f = false, b;

6.b = (t && ((i++) == 0));

7.b = (f && ((i+=2) > 0));

8.System.out.println(i);

}

}

answer is 1.

2.1.public static ShortCkt {

2.public static void main(String args[]) {

3.int i = 0;

4.boolean t = true;

5.boolean f = false, b;

6.b = (t & ((i++) == 0));

7.b = (f & ((i+=2) > 0));

8.System.out.println(i);

}

}

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.

2.public static void main(String args[]) {

3.int i = 0;

4.boolean t = true;

5.boolean f = false, b;

6.b = (t && ((i++) == 0));

7.b = (f && ((i+=2) > 0));

8.System.out.println(i);

}

}

answer is 1.

2.1.public static ShortCkt {

2.public static void main(String args[]) {

3.int i = 0;

4.boolean t = true;

5.boolean f = false, b;

6.b = (t & ((i++) == 0));

7.b = (f & ((i+=2) > 0));

8.System.out.println(i);

}

}

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

Posts: 30

posted 8 years ago

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

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

SCJP aspirant<br />SCWCD aspirant

Manfred Klug

Ranch Hand

Posts: 377

posted 8 years ago

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.

- 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

Posts: 148

I agree. Here's the link: http://aspose.com/file-tools |