0

I am trying to figure out how this boolean expression is evaluated:

int i = 0;
boolean exp = i > 1 && counter();

In the following code:

public class Main {

    static int num = 0;

    public static boolean counter(){   
            num++;
         if(num == 6){
             return true;
         }
          return false;
     }
    public static void main(String[] args) {
            //int i = 2;
              int i = 0;
            boolean exp = i > 1 && counter();
            System.out.println(num); // 0
            System.out.println(exp); // false
    }   
 }

Since i = 0, i > 1 is false and counter() returns false. However, I'm confused because the value of static variable num doesn't change despite the counter() function being in the boolean expression:

exp = i > 1 && counter();

I was expecting the output to be

1
false

but it is

0
false

Why does counter() not seem to be called?

Justin
  • 24,288
  • 12
  • 92
  • 142
Oghli
  • 2,200
  • 1
  • 15
  • 37
  • 3
    Due to [logical short-circuiting](https://en.wikipedia.org/wiki/Short-circuit_evaluation), `counter()` doesn't get evaluated if `i > 1` returns `false`. – Phylogenesis Jun 07 '17 at 13:31
  • @Phylogenesis Does evaluating like this expression depends on the programming language in that case is this way of evaluating change from language to another. – Oghli Jun 07 '17 at 14:00
  • 1
    @Colt As per my link, this behaviour is programming language dependent. Logical operators in Visual Basic (not VB.Net) do not short-circuit like this, for instance. – Phylogenesis Jun 07 '17 at 14:04
  • @Phylogenesis Thanks for info. very helpful. – Oghli Jun 07 '17 at 14:06

3 Answers3

3

This is because && is short-circuiting. When i > 1 evaluates to false, there is no need to check the other side of the logical AND because the resulting expression will always be false. Therefore, the method counter is never run.

To fix this, you can either move counter() to be the first condition, or use the non-short-circuiting &.

Zircon
  • 4,677
  • 15
  • 32
  • Thanks alot ! that make sense. in this case it doesn't even execute counter function so num value still the same! – Oghli Jun 07 '17 at 13:37
  • does evaluating like this expression depends on the programming language in that case it doesn't evaluate the other side if the left side is already false – Oghli Jun 07 '17 at 13:42
  • 1
    @Colt well, the expressions themselves could differ by language, but in Java and many other OOP languages, `&&` and `||` are _short-circuiting operations_ which by definition behave in that way. – Zircon Jun 07 '17 at 14:04
2

&& operator evaluates the right side expression if the left side expression is true.

If any one of the expressions is false then the entire expression would be false in the && operator case -> no need to evaluate the other expressions. So the counter() expression is not at all executed in your case.

0 > 1 fails..

Hence counter() is not at all evaluated.

idarak
  • 126
  • 6
0

It's because of && is quick boolean operation. If first operand is false then it will return false immediately without evaluating second operand. You can use &, it will always evaluate both operands. (also & is a bit-wise operator if the operands are integral.) The same thing with logical OR : ||. if the first operand is true, it will immediatly retrun true.

R Dhaval
  • 536
  • 1
  • 9
  • 21