Java Programming - Operators and Assignments - Discussion

8. 

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 3
[B]. 8 2
[C]. 8 3
[D]. 8 5

Answer: Option B

Explanation:

The first two iterations of the for loop both x and y are incremented. On the third iteration x is incremented, and for the first time becomes greater than 2. The short circuit or operator || keeps y from ever being incremented again and x is incremented twice on each of the last three iterations.


Akash said: (Nov 1, 2010)  
Just explain output of this type of code through steps.

Susanta Pradhan said: (Jun 11, 2011)  
Please explain in for loop.

Maheshthakuri said: (Jun 30, 2011)  
1st Iteration
z =0
x=1 y=1

2nd Iteration
z=1
x=2 y=2

3rd Iteration
z=2
x =3 and its greater than 2. Go inside the if condition
x become 4
y remain 2 as coz of || operator(if one condition true other is ignored)

4th Iteration
z=3
again condition true
x become 6 coz of ++x and x++.
y remain same as stated above

5th Iteration
z=4
again condition true(x>2)
x become 8 now
y remain same as stated in iteration 3rd

6th Iteration
z = 5. condition become false coz z is not less than 5

output : 8 and 2

Program terminate

Jayaram.... said: (Feb 16, 2012)  
Hi Thanks, Mr.Maheshthakuri

Mohamed Jaleel Nazir said: (Aug 22, 2014)  
for(int z=0;0<5;z++)->if((1>2)||(1>2))
for(int z=1;1<5;z++)->if((2>2)||(2>2))
for(int z=2;2<5;z++)->if((3>2)||(2>2))

// 'if' true then get into braces
x++; // x=4

for(int z=3;3<5;z++)->if((5>2)||(2>2))

// 'if' true then get into braces
x++; // x=6

for(int z=4;4<5;z++)->if((7>2)||(2>2))

// 'if' true then get into braces
x++; // x=8

for(int z=5;5<5;z++) -> condition false then get out of loop.
Now x=8.

Then x=8 y=2.

Murali said: (Feb 4, 2015)  
1----> x--1, y--1 false.

2----> x--2, y--2 false.

3----> x--3, y--3 true.

x--4, y--3.

4---> x--5, y--4 true.

x--6, y--4.

5----> x--7, y--5 true.

x--8, y--5.

There fore (x, y) = (8, 5) option D is correct.

Manoj said: (Mar 24, 2015)  
I too think option D is correct. Because the value of 'y' keep increasing.

Problem with option B: If value of y will incremented up to 2 then it will increment up to end.

Summa said: (May 27, 2015)  
I thought "||" means "or" which means that, their can be both condition true, or one is true, the loop start to begin. In that case I think "D" is correct.

Purushottam said: (Apr 15, 2016)  
Thanks @Maheshthakuri.

Ravi said: (May 24, 2016)  
How can you people understand this programs?

I completed BCA but I can't understand this programs.
For example I write small program here :

class Fact
{
public static void main(String [] args)
{
int n,c,fact=1;
S.o.p("enter a number to find fact");
Scanner in=new Scanner(System.in);
n=in.nextInt();
if(n<0)
s.o.p("enter number should be not a negative");
else
{
for(c=1;c<=n;c++)
fact=fact*c;
s.o.p('factorial of given number :"+fact);
}
}
}

Could anyone please explain this program briefly one by one line.

Anand Patil M said: (Mar 16, 2017)  
Enter a number to find fact.
3
n store 3
if(3<0)
no so 3 is not a negative number
then else execute
for()

The first iteration take 1 value is less then equal 3
yes
fact=1*1
fact store 1
next iteration
2is less then equal to 3
yes
then
fact=1*2;
fact store 2
3rd iteration
3 is less then equal to3
yes then
fact=2*3
then fact store 6.

fourth iteration fail then print 6 value.

Arun said: (Jun 9, 2017)  
Guys, When we use || logic operator, second condition will be executed only when the first condition is false.

Saurabh said: (Jun 24, 2017)  
In simple word, in Logical OR, if the first condition is true then second will be ignored.

So here the condition of 'y' is checked only for 2 times.

Malathi said: (Jan 22, 2018)  
Thanks @Maheshthakuri.

Priyanka said: (Mar 13, 2018)  
Thank You @Arun.

Neha said: (Nov 22, 2018)  
Thanks @Maheshthakuri.

Mayur Patil said: (Jun 11, 2019)  
Thanks @Maheshthakuri.

Sanjay Kumar said: (Nov 15, 2020)  
Thanks @Maheshthakuri.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.