• Post Reply Bookmark Topic Watch Topic
  • New Topic

Please someone explain these codes.  RSS feed

 
Mohammad Nizam Uddin
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question was asked in a java test



The result they have given is 1 1. Please someone explain it to me. what will be the result if i put the condition if (++a == 1 || ++b == 1).

I tried the code but i see no result. My code is given below.

 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammad Nizam Uddin wrote: . . .
The result they have given is 1 1. . . .
That output they gave is incorrect. Please tell us where that question comes from. Have a look at our FAQ and see whether that explains the problem to you. Also see if you can work out for yourself what would happen if you used ++a and ++b instead.
 
Mohammad Nizam Uddin
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Mohammad Nizam Uddin wrote: . . .
The result they have given is 1 1. . . .
That output they gave is incorrect. Please tell us where that question comes from. Have a look at our FAQ and see whether that explains the problem to you. Also see if you can work out for yourself what would happen if you used ++a and ++b instead.


This question was asked in Elance.com free Java test. As I have told before, my code shows nothing. just blank. can you tell me why? I tried the below code
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammad Nizam Uddin wrote:As I have told before, my code shows nothing. just blank. can you tell me why?

Well, because:
1. "a" initial value is 0
2. Post increment a++ in your case means check equality, then increment

So, what you get is:
Cambell already gave you a hint, what would happen if you'd change a++ to ++a?

edit: note, that "a" and "b" after expression evaluates (within "if" condition check), both has values 1.
 
Mohammad Nizam Uddin
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Mohammad Nizam Uddin wrote:As I have told before, my code shows nothing. just blank. can you tell me why?

Well, because:
1. "a" initial value is 0
2. Post increment a++ in your case means check equality, then increment

So, what you get is:
Cambell already gave you a hint, what would happen if you'd change a++ to ++a?

edit: note, that "a" and "b" after expression evaluates (within "if" condition check), both has values 1.


Thanks for your explanation. but if I initialize a = 1; b = 1; then according to your explanation 'a' should be incremented by 1, so now a = 2, similarly b should be incremented by 1 and b = 2 as well. So, the result should be 2 2. But surprisingly when I run the code, I get the result 2 1. Please can you tell me why?
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Mohammad,

if you have the statement:

is b evaluated when it is found that a is true?

Greetz,
Piet
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammad Nizam Uddin wrote:So, the result should be 2 2. But surprisingly when I run the code, I get the result 2 1.

The word "result" you're using is rather confusing.

Don't get confused about these:
1. You have initial value, for ex. "a = 0"
2. You have value "a++ == 0" where "a" still 0
3. And you have value after expression is evaluated, "a = 1"

Show the exact code you ran and it should clear things up, where you got confused.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agh, Piet Souris touched a very good point. But there is another topic you'll need to understand.
At the moment probably enough to know what Piet has in mind - try to figure out.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Liutauras,

I only had a quick glance, and so I 'm curious to know what that topic might be.
I'll leave it further up to you, since you are much better in explaining these
things than I am.

Greetz,
Piet
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I don't think I'm better in explaining, as a proof, my wrongly used word "topic".
I meant there is bitwise operator used with boolean values, which could lead to a different result in OP's case.
But just didn't want to confuse him, so chose word "topic", which probably raised your curiosity

addition: sorry, my bad
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammad Nizam Uddin,

What Piet mentioned above, you could read about it in here (<- link).
"The Conditional Operators" topic. Read it very carefully and think about Piet's question.
 
Mohammad Nizam Uddin
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually this is what I thought in the first place. I thought that the second condition (b++==1) will not be evaluated if the first condition (a++==1) is met.
Therefore, a is incremented by 1, so a = 2. but b is never incremented so b remains 1. The output is 2 1.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right about that, but, be careful.

(1 == 1 || b++ == 1) // second expression not being evaluated
(1 == 0 || b++ == 1 ) // second expression being evaluated
(1 == 1 && b++ == 1) // second expression being evaluated
(1 == 0 && b++ == 1) // second expression not being evaluated
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My, that bit of code is even worse than I thought. I forgot about the side‑effects being missed by the || operator.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:My, that bit of code is even worse than I thought. I forgot about the side‑effects being missed by the || operator.
What do you mean?
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may only execute a++ and never execute b++, which is how you get the output 2 1. It shows how bad it can be to use && and || when there are side‑effects to either of its operands. The side‑effect is of course the i++ operation.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I said, “that bit of code,” I meant the original post with
if (a++ == 1 || b++ == 1) ...

Anyway, we have consensus that I was right to say the answer given for that exercise was incorrect.
 
Mohammad Nizam Uddin
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:When I said, “that bit of code,” I meant the original post with
if (a++ == 1 || b++ == 1) ...

Anyway, we have consensus that I was right to say the answer given for that exercise was incorrect.


Exactly. I didn't expect Elance.com to provide incorrect answers for their tests. Anyway this is what one should expect in a free test. LOL!
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you would expect correct answers for what is actually a very simple piece of code. It took me only a few seconds to see that it would never print 1 1.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!