Java Programming - Operators and Assignments - Discussion

7. 

What will be the output of the program?

class Test 
{
    public static void main(String [] args) 
    {
        int x= 0;
        int y= 0;
        for (int z = 0; z < 5; z++) 
        {
            if (( ++x > 2 ) && (++y > 2)) 
            {
                x++;
            }
        }
        System.out.println(x + " " + y);
    }
}

[A]. 5 2
[B]. 5 3
[C]. 6 3
[D]. 6 4

Answer: Option C

Explanation:

In the first two iterations x is incremented once and y is not because of the short circuit && operator. In the third and forth iterations x and y are each incremented, and in the fifth iteration x is doubly incremented and y is incremented.


Magesh said: (Sep 28, 2010)  
Can any one please mention what is short circuit operator?

Divya said: (Jun 2, 2011)  
There are 2 types of short circuit operators.

1. Short circuit AND (&&)
2. Short circuit OR (||)

Now in the and case if first condition is false then the second is never evaluated as true and false gives you false only. In short circuit OR if first condition is true the second one is never evaluated as true OR false gives you true only. So its basically a short cut.

Sudhanshu said: (Nov 20, 2012)  
Please explain this example practically.

Narayana Murthy said: (Dec 23, 2012)  
@Sudhanshu

The problem is with the two conditions in "if".
If one condition fails then "if" never go for the other condition.

In the example:

In the first iteration, it goes in to the "if" and first increments "x" and checks the condition.. it fails. then it wont go for the next condition to check(we used "&&" so both the conditions should be true to enter into the "if" block.

next iteration will also fails (Coz 2>2 fails)

In third iteration "x" will be 3. but "Y" incremented by 1 so "y=1"(again fails 1>2)

In fourth iteration "x" will be 4 but still the second condition is not satisfied "y=2" only (2>2 fails).

In fifth iteration "x" will be 5 and "y" will be 3, condition true so enters into "if" and increments x by 1. so x=6.(6>2 and 3>2)

finally x=6 and y=3..

Saranya said: (Dec 29, 2012)  
for(int z=0;0<5;z++)->if((1>2)&&(0>2))
for(int z=1;1<5;z++)->if((2>2)&&(0>2))
for(int z=2;2<5;z++)->if((3>2)&&(1>2))
for(int z=3;3<5;z++)->if((4>2)&&(2>2))
for(int z=4;4<5;z++)->if((5>2)&&(3>2))

Now x=5
So,x++=6
Now x=6 y=3.

Vinoth said: (Jul 1, 2013)  
I'm still confusing because "if" condition is failed then the x would not be incremented, It would come out from the loop. Explain me please?

Thanvir.Cse said: (Jul 11, 2013)  
if (( ++x > 2 ) && (++y > 2)) here x's and y's incrementation does not depend on "if" condition.

When(until for loop condition is true) execute that block i mean "if (( ++x > 2 ) && (++y > 2))" then always x will be incremented.

While first condition is true then check second one(as rules of Short circuit AND (&&) ).

That means y will be incremented. And when for(int z=4;4<5;z++)->if((5>2)&&(3>2)){ x++ /* x=5+1*/ }.

Rohit said: (Dec 29, 2013)  
Hi,

What will happen ++(increment operator) preceding & proceeding the variable?

What will happen if in above sum it is :

if (( x++ > 2 ) && (y++ > 2))

Instead of,

if (( ++x > 2 ) && (++y > 2))

And I think the solution described by @Saranya is wrong as it will compare the variable & after that it will increment the variable(i.e after the iteration is completed).

Arnold Villasanta said: (Aug 10, 2014)  
z      ++x              ++y           x++            x       y
0 executed not executed not executed 1 0
1 executed not executed not executed 2 0
2 executed executed not executed 3 1
3 executed executed not executed 4 2
4 executed executed executed 6 3
5 (loop stop)

Uma said: (Nov 15, 2015)  
Explain me clearly I did not get?

Peacz said: (Jan 20, 2016)  
@Uma you can see @Narayana Murthy explain.

He explain so clear.

G.Vivek said: (Apr 22, 2016)  
Short-circuit(&&):

(x&&y) -> X is false, then Y not evaluated. X is true, then Y will be evaluated.

Above problem's Solution is:

for(int z=0;0<5;z++)->if((1>2)&&(0>2))..X is false then Y not evaluated
for(int z=1;1<5;z++)->if((2>2)&&(0>2))..X is false then Y not evaluated
for(int z=2;2<5;z++)->if((3>2)&&(1>2))..X is true then Y is evaluated
for(int z=3;3<5;z++)->if((4>2)&&(2>2))..X is true then Y is evaluated
for(int z=4;4<5;z++)->if((5>2)&&(3>2))..X is true then Y is evaluated

Finally, (X and Y both as true then X value was incremented)

=> X is 6
=> Y is 3

If(X&&Y) //X and Y both true then
{
X++; // X value is incremented
}

Nataliia said: (Jul 5, 2016)  
class Main {
public static void main(String[] args) {
int x = 0;
int y = 0;
for (int z = 0; z < 5; z++) {
System.out.println("Before if: " + x + " " + y);
if ((++x > 2) && (++y > 2)) {
System.out.println("Before inc: " + x + " " + y);
x++;
System.out.println("After inc: " + x + " " + y);
}
System.out.println("After if: " + x + " " + y);
}
System.out.println("Result : " + x + " " + y);
}
}

Console:

Before if: 0 0
After if: 1 0
Before if: 1 0
After if: 2 0
Before if: 2 0
After if: 3 1
Before if: 3 1
After if: 4 2
Before if: 4 2
Before inc: 5 3
After inc: 6 3
After if: 6 3
Result : 6 3

Aashish said: (Nov 26, 2016)  
Thanks for your explanation @Arnold Villasanta.

Sunny Deol said: (Jan 21, 2017)  
Now I understand this, thank you @Nataliia.

Rajesh said: (May 27, 2017)  
Awesome, thanks @Vivek.

Gajendra Chouriya said: (Jun 29, 2017)  
Thanks for the explanation @G.vivek.

Sai Kiran said: (Jul 23, 2017)  
Awesome explanation @Vivek.

Udaya said: (Oct 24, 2017)  
Good explanation, Thanks @G. Vivek.

Avni said: (May 16, 2018)  
Why y =0 in first case, why not 1?

Aayushi Jain said: (Jul 26, 2018)  
Why y is not get executed?

Preethi said: (Aug 3, 2018)  
Thank you good explanation @G.Vivekananda.

Aparna said: (Sep 17, 2018)  
Hi, Please explain this problem using OR.

Swapnil said: (Nov 14, 2018)  
Thank you so much @Arnold.

Niharika Patidar said: (Jun 23, 2019)  
Please explain to me.

What is the reason that ++y is not executed for the first two times?

Anu said: (Sep 19, 2019)  
Thanks @Saranya and @G. Vivek.

Unknown said: (Nov 26, 2019)  
output of int z=2; while(z<20) if((z++)%2==0) system.out.println(z);

Moha said: (Apr 5, 2021)  
x=1 >2 wrong
z++
z=1
x=2>2 wrong
z++
z=2
x=3>2 yes
&&
1>2? wrong
z=3
x=4
2>2 rong
z=4
x=5
3>2 yes.

x++ x=6.
So, o/p: 6 3.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.