• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in K&B concerning topic :Operators

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am new to this forum.I am preparing for scjp5.The following question is from K&B chapter 4 Question no.5.

class Foozit{
public static void main(String args[]){

Integer x = 0;
Integer y = 0;

for(Short z =0; z < 5; z++)

if((++x > 2) || (++y > 2));
x++;
System.out.println(x + " " + y);

}
}

What is the result?

A. 5 1
B. 5 2
C. 5 3
D. 8 1
E. 8 2
F. 8 3
G. 10 2
H. 10 3


The answer marked in the K&B is E.But when I compiled and run this program I got the result as 6 2.

Here,after the if statement the x is not an expression just a postincrement of x.(Is it because of that?)

Please correct me if I am wrong?

Thanks
Neju.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

I don't have the book with me, but I think I see your problem. You have a semicolon at the end of your "if" condition. And because of this "empty" statement, your code is doing this...

...when it should be doing this...

Please check the book and let us know if that solves it.

(I'm not sure what you're asking about the line x++. Yes, it is a postincrement operation on x, but it is also an expression that evaluates to the value of x before being incremented.)
 
Najmunnisa Sageer
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marc,

You are right,In the book there is no semicolon.(I added by mistake that semicolon).Its working perfectly and understood the concept also.


Thanks
Neju.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello everybody,

I have the same exercise to do, but i do not understand how can we get the result 8 2
Could someone explain? txs
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

for (Short z = 0; z < 5; z++){ // line1
if ((++x > 2) || (++y > 2)){ // line2
x++; // line3
}
}
System.out.println(x + " " + y);

Here there are three conceptual things to be noted. One thing is, short-circuit ||, pre increment ++x and post-increment x++

line1 - for loop which iterates for 5 times that is 0 to 4
line2 - checks if x is greater than 2 with a short-circuit || and if its false then only the other operand on the right hand will get executed
line 3 - if line2 evalutes to true then this line will be executed (post-increment x++)


The below shows the values for ++x,++y,z and x++ at every iteration of the for loop.
z|x++|++x|++y
0|- |1 |1
1|- |2 |2
2|4 |3
3|6 |5
4|8 |7


Hope this helps.

Kris
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
only because of semicolon at end of if statement
output are different
when semicolon is present at end of if then output is 6 2
when semicolon is not present at end of if then output is 8 2

I think every one should note it


Regards
Ninad
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic