-2

Here is the code and i'm really confusing about the output.

public static void main(String[] args) {
    int a = 1;
    int b = 1;

    for (int c = 0; c < 5; c++) {
        if ((++a > 2) || (++b > 2)) {
            a++;

        }
    }
    System.out.println(a + " " + b);
}

The output is 10 2

However, why could a always change but b remains the same?

  • 1
    It is because of the or condition. If one expression in left gets evaulted to true, the remaining part of the expression does not get evalutated. – NewUser Jan 05 '17 at 07:19

4 Answers4

4

The OR expression inside the if is getting short-circuited. When the following expression is being evaulated:

(++a > 2) || (++b > 2)

The term with b is only evaluated if the a term is false.

In the first iteration, the a term fails, and the b term is evaluated. After this first iteration, both a and b are equal to 2. For every subsequent iteration, the a term is always true, and the b term therefore is never even evaluated.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • And a link to the specs, if anybody is curious: http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.24 "[||] evaluates its right-hand operand only if the value of its left-hand operand is false." – Jason Hoetger Jan 05 '17 at 08:05
  • 1
    @NewUser I don't know why the two users downvoted, but I guess it is because Tim and Eran prefered to answer instead of closing as a duplicate. Eran is known for farming reputation whenever possible, but Tim may should know better. Btw *"Anyways voted up to counter the negative votes."* is a bad reason for voting. Vote on the answer and if you like it, not to "undue" other votes. – Tom Jan 05 '17 at 09:11
  • @Tom Part of the reason why many farm reputation nowadays is the slew of bad Java questions coming onto the site. This question, while likely a duplicate of something out there, was relatively good, hence so many people trying to give answers. – Tim Biegeleisen Jan 05 '17 at 09:15
  • 1
    @TimBiegeleisen The quality of the question decides if you up- or downvote, but a vote to close depends on if the question is answerable and not a duplicate. If one doesn't care about the last one, then he can still answer, but others may don't like that behaviour. And btw "many people": there are 4 answers. 2 by people with experience on this site (these answers have downvotes) and 2 by people without much experience (judging by their rep) and they don't have downvotes. Maybe a coincidence? Or the voters expect more from you and Eran. – Tom Jan 05 '17 at 09:46
  • @Tom : I am relatively inexperienced as well, I had no idea about "farming reputation". Got to do some catch up then! – NewUser Jan 05 '17 at 09:59
  • 2
    @NewUser You might want to check http://meta.stackoverflow.com/ and http://meta.stackexchange.com/. Both sites are for discussing stuff about Stack Overflow (first link) or Stack Exchange in general (second link). You may find some useful questions by searching for "help vampire" (people who ask bad questions without caring much for quality or this site) and "rep farming" [there's another name, which can't be written in the comments] (people who do stuff just to increase their reputation stats). If and which case we have here in this question can everyone decide themselves. – Tom Jan 05 '17 at 10:20
  • @Tom Hilarious: rep \u0077whores and help vampires...sadly there are way too many of these people on the site, really from all walks of life (some with 100 reps, some with 100K reps). Thanks for all this information. – Tim Biegeleisen Jan 05 '17 at 10:22
1

|| is a short circuited operator.

Therefore (++a > 2) || (++b > 2) will only evaluate the second operand (++b > 2) if the first operand (++a > 2) is false.

Once a reaches 3, b is no longer incremented.

Eran
  • 387,369
  • 54
  • 702
  • 768
0

|| is short circuited operator and that means if the first condition is true it will not check the second condition because or statement only need one true condition so at first time "++a = 2" and that is not > 2 so it's false so it checks the second condition ++b = 2 then "++a = 3" and it's > 2 so it's true so it will not check the second condition and same when" a = 4 to 10"

so if you want it to check the b also you have to change from

if ((++a > 2) || (++b > 2)) 

to

if ((++a > 2) | (++b > 2)) 
Amado
  • 371
  • 1
  • 16
  • I wouldn't recommend the latter, just because it could be confusing to a reader. If you really want to increment both variables, just do it: `a++; b++; if (a > 2 || b > 2) ...`. You don't get points for cramming everything onto one line. – ajb Jan 05 '17 at 07:28
  • I know that but when I answer like this he will not get the point of how to access the second condition if he needed it in future – Amado Jan 05 '17 at 07:29
  • So "|" also means or but it is not a short circuited operator? – James Richard Lee Jan 05 '17 at 07:48
  • Yup it's not and same with && , & – Amado Jan 05 '17 at 07:51
  • could you be more specific? how will & work? – James Richard Lee Jan 05 '17 at 07:54
  • The same way as | but the difference is & statement need only one false condition so If the first condition was false in && so the next will not be checked cuz if one condition is false then the whole conditions are false but when using & whatever the first condition is it will check the second – Amado Jan 05 '17 at 07:57
  • Thx a lot! Now I'm clear! – James Richard Lee Jan 05 '17 at 08:51
  • you welcome .. :) – Amado Jan 05 '17 at 09:18
0

you can debug your program to trace a and b variables and get what happens exactly at each iteration :

a=1 ; b=1;

when the logical operator is || if compiler found first operand false he will go to the next operand to check it so the ++a and ++b will be both executed in first iteration so a=2; b=2;

now next iteration compiler find first operand a=3 is true and it will not go to the next operand to check it so value of b will not increase by 1 and stay b=2

a=3; b=2;

next iteraion also in the same way until finish of loop so you will get output:

a=10
b=2

check this program to understand how || operator work :

    int a = 1;
    int b = 1;
    int d = 1;
    for (int c = 0; c < 5; c++) {
        if ((++a > 2) || (++b > 2) || (++d > 2)) {
            a++;  
        }
    }

when c=1 variables will be:

a =2 
b =2 
d =2

next iteration c=2 variables will be:

a=4
b=2
d=2

in this iteration compiler will only check first operand a=3 which is true and it will not check others operands so its values will stay the same b=2 and d=2

next iteration c=3 variables will be:

a=6
b=2
d=2

and so on until finsh of loop output :

a=10
b=2
d=2 
Oghli
  • 2,200
  • 1
  • 15
  • 37