Java Bitwise Operators Logical Operators Question 1

Question

What is the output of the following code?


public class Main {
  public static void main(String args[]) {
    int c = 0;//w  w  w. j a v  a 2 s  .c  o  m
    int e = 100;
    int d = 0;
    if (c == 1 & e++ < 100) {
      d = 100;
    }

    System.out.println(c);
    System.out.println(e);
    System.out.println(d);
  }
}


0
101
0

Note

Even though c==1 return false, the e++<100 part is still running.

Using a single & ensures that the increment operation will be applied to e whether c is equal to 1 or not.




PreviousNext

Related