Java Programming - Operators and Assignments - Discussion

Discussion Forum : Operators and Assignments - Finding the output (Q.No. 7)
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);
    }
}
5 2
5 3
6 3
6 4
Answer: Option
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.

Discussion:
30 comments Page 3 of 3.

Peacz said:   10 years ago
@Uma you can see @Narayana Murthy explain.

He explain so clear.

Uma said:   10 years ago
Explain me clearly I did not get?

Arnold villasanta said:   1 decade ago
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)

ROhIt said:   1 decade ago
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).

Thanvir.cse said:   1 decade ago
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*/ }.

Vinoth said:   1 decade ago
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?

Saranya said:   1 decade ago
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.

Narayana murthy said:   1 decade ago
@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..

Sudhanshu said:   1 decade ago
Please explain this example practically.

Divya said:   1 decade ago
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.


Post your comments here:

Your comments will be displayed after verification.