This week's giveaway is in the Spring forum.We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of REST with Spring (video course) this week in the Spring forum!

SCJP Operator question

ali kamran
Greenhorn

Joined: Apr 25, 2010
Posts: 29
Hello,
Running the below code.May some one please explain how and why the output is 4.First if check its try increments i to 2.Second if statement both true does not increment.so why is the answer 4 ?

Thank you all for time and patience

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 20380

50

ali kamran wrote:Hello,
Running the below code.May some one please explain how and why the output is 4.First if check its try increments i to 2.Second if statement both true does not increment.so why is the answer 4 ?

Thank you all for time and patience

Neither "if" condition resolves to true, so neither increment in the "if" body is actually executed. However, there are three increments within the condition itself, and these increments do get executed.

Henry

Bruno Candido Volpato da Cunha
Greenhorn

Joined: Feb 21, 2012
Posts: 18

The XOR (eXclusive or) validates both sentences in IF.

3 increments, as Ali said.

Bruno Candido Volpato da Cunha
Oracle Certified Java Programmer 6
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
ali kamran wrote:

Here is how the code is implemented:
Step 1: x=1, (4>1) is true , x=2, x+2=4, (4>3) is true. True ^ True is 1^1 which is false.
Step 2: x=2, ++x will make x=3, (4>3) is true. ++x makes x = 4. (4==5) is false. But !(4==5) is true. True ^ True is a false.
Step 3: x is 4 .
I hope this will help.
ali kamran
Greenhorn

Joined: Apr 25, 2010
Posts: 29
Helen Ma wrote:
ali kamran wrote:

Here is how the code is implemented:
Step 1: x=1, (4>1) is true , x=2, x+2=4, (4>3) is true. True ^ True is 1^1 which is false.
Step 2: x=2, ++x will make x=3, (4>3) is true. ++x makes x = 4. (4==5) is false. But !(4==5) is true. True ^ True is a false.
Step 3: x is 4 .
I hope this will help.

Thank you all ..much appreciated and crystal clear..thank you ..
ali kamran
Greenhorn

Joined: Apr 25, 2010
Posts: 29
Helen Ma wrote:
ali kamran wrote:

Here is how the code is implemented:
Step 1: x=1, (4>1) is true , x=2, x+2=4, (4>3) is true. True ^ True is 1^1 which is false.
Step 2: x=2, ++x will make x=3, (4>3) is true. ++x makes x = 4. (4==5) is false. But !(4==5) is true. True ^ True is a false.
Step 3: x is 4 .
I hope this will help.

If I may ask a follow up question to clarify the concept.We have code below.

int z = 5;

if (++z < 5 & ++z > 6 )
{
System.out.println(++z);
}

here nothing is being printed.Walking through the code. in first part of comparison ++z <5 becomes 6<5 not true .so move to next bit which is ++z >6 now z is 7 .7 is greater than 6.so why does it not go inside the loop ?
Thanks again .
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451

int z = 5;

if (++z < 5 & ++z > 6 )
{
System.out.println(++z);
}

here nothing is being printed.Walking through the code. in first part of comparison ++z <5 becomes 6<5 not true .so move to next bit which is ++z >6 now z is 7 .7 is greater than 6.so why does it not go inside the loop ?
Thanks again .

Yes. ++z = 6 , 6< 5 is false. ++z = 7 and 7> 6 which is true. But false & true is a false. Then, the if statement is not executed and so nothing is printed.

I agree. Here's the link: http://aspose.com/file-tools

subject: SCJP Operator question