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.

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

Gajendra chouriya said:   8 years ago
Thanks for the explanation @G.vivek.

Udaya said:   8 years ago
Good explanation, Thanks @G. Vivek.

Avni said:   7 years ago
Why y =0 in first case, why not 1?

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

Anu said:   6 years ago
Thanks @Saranya and @G. Vivek.

Sai kiran said:   8 years ago
Awesome explanation @Vivek.

Aayushi jain said:   7 years ago
Why y is not get executed?

Swapnil said:   7 years ago
Thank you so much @Arnold.

Rajesh said:   8 years ago
Awesome, thanks @Vivek.


Post your comments here:

Your comments will be displayed after verification.