• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SCJP Operator question

 
ali kamran
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: Added code tags]
 
Henry Wong
author
Marshal
Pie
Posts: 21518
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
Eclipse IDE Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The XOR (eXclusive or) validates both sentences in IF.

3 increments, as Ali said.
 
Helen Ma
Ranch Hand
Posts: 451
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 451
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic