aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in K&B concerning topic :Operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in K&B concerning topic :Operators" Watch "Doubt in K&B concerning topic :Operators" New topic
Author

Doubt in K&B concerning topic :Operators

Najmunnisa Sageer
Greenhorn

Joined: May 18, 2008
Posts: 9
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.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Najmunnisa Sageer
Greenhorn

Joined: May 18, 2008
Posts: 9
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.
Ahlem Ouertani
Greenhorn

Joined: Jun 20, 2008
Posts: 1
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
Krishnamoorthy Vuyala Muralidharan
Ranch Hand

Joined: Sep 13, 2005
Posts: 52
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
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 802

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


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in K&B concerning topic :Operators