Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
double j = 128.22, k = 129.22;
if ((j++) == k)
    j += 10;
k += 10;
System.out.println(j);
System.out.prinkln(k);


The output is 129.22
139.22

Why does it read the if statement as false? I'm teaching programming 1 and I've asked all my sources, and can't really get an answer.
Posted
Updated 16-Oct-14 6:06am
v2
Comments
[no name] 16-Oct-14 12:04pm    
Because that is the way the postfix operator works. http://msdn.microsoft.com/en-us/library/aa691363(v=vs.71).aspx
Richard MacCutchan 16-Oct-14 12:29pm    
How can you be teaching programming if you do not understand something as basic as this? I suggest you go to http://download.oracle.com/javase/tutorial/index.html and study the language in greater detail.
Member 11158840 16-Oct-14 20:20pm    
You know, you might want to think before you post something so rude. I happen to be teaching the class because nobody else was brave enough to try to learn JAVA and teach it in my school. So I'm learning it and teaching it at the same time so the 29 kids who signed up for it can actually take it. No, I'm not an expert, but obviously I'm trying my best since I came to this forum to make sure I knew the correct explanation for this situation which wasn't fully explained in the three textbooks I've been studying, or the online tutorials I've watched.
Richard MacCutchan 17-Oct-14 4:23am    
That's not rude it's common sense. If you do not even understand the basics then you should not be teaching it. The chances are quite high, given your question, that you are going to be teaching your class quite a few wrong things. And given the number of samples of really bad code we see in these forums I suspect there are other equally poorly trained teachers around. Think about what might happen if one of your students is tasked with writing an aircraft navigation system and does not understand the difference between i++ and ++i.
Member 11158840 16-Oct-14 14:05pm    
I'm teaching it for the first time. I'm a math person, not a programmer, this is new to me. I understood the prefix/postfix for print, but I wanted to confirm it also worked the same way for the if statement. Believe me I'm studying it as much as possible! (Also I asked the teacher who taught it for years and he also wasn't sure... )

1 solution

Look into the difference between.
Java
if ((j++) == k)     // postfix operator

and
JavaScript
if ((++j) == k)  // prefix operator


For the postfix operator the comparison is made BEFORE j is incremented.
(128.22 == 129.22)

For the prefix operator the comparison is made AFTER j is incremented.
(129.22 == 129.22)

[Update]
As Sergey mentioned, it is pretty unusual to use the ++ operator on floating point types. Floating points are a bit complicated and can be tricky to explain.
As we touched on this subject I add two links for further reading:
Look at Solution 5 for this CP question. The problem with floating numbers[^]

As a math/programming teacher, you might find this article interesting. What Every Computer Scientist Should Know About Floating-Point Arithmetic[^]

Good luck with your teaching.
 
Share this answer
 
v3
Comments
Member 11158840 16-Oct-14 12:15pm    
Thank you!!! I knew it had something to do with that, but I couldn't figure out how. Now I can explain it to my students!
Sergey Alexandrovich Kryukov 16-Oct-14 17:18pm    
Sure, as simple as that, my 5.
I would add:
1) using ++ operators with double is pretty weird;
2) combination of operators with conditional statement and assignment looks laconic and cool, but isn't clarity and readability more important? I would advise: keep it simple (I don't want to say KISS, because I don't want to say OP is "S" :-)
—SA
George Jonsson 17-Oct-14 5:53am    
I added some links that explains floating points. I hope you find them helpful.
Sergey Alexandrovich Kryukov 16-Oct-14 17:18pm    
Sure, as simple as that, my 5.
I would add:
1) using ++ operators with double is pretty weird;
2) combination of operators with conditional statement and assignment looks laconic and cool, but isn't clarity and readability more important. I would advise: keep it simple (I don't want to say KISS, because I don't want to say OP is "S" :-)
—SA
Member 11158840 16-Oct-14 20:24pm    
I agree with you 100%.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900