10

I was just asked a question in a technical interview that I was a bit confused about.

The question was as follows:

If

int i = -1, int j = -1, and int k = -1, 

and we run the following line:

++i && ++j && ++k

what would be the new values of i, j, and k? The reason I was confused is that, since we are not assigning this expression to anything, it doesn't seem like the and operators should make any difference (only the increment operators should). However, running a simple test program quickly proved that I was mistaken. Could someone explain this to me, as I have never seen this exercise before.

John Roberts
  • 5,885
  • 21
  • 70
  • 124

2 Answers2

20

The key here is that && is short-circuiting.

So, ++i is evaluated first. It increments i and returns the new value, which is 0, so the rest of the expression doesn't get evaluated.

The values should be 0, -1, -1 if I'm not mistaken.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • I see now. All the interviewer had to mention was the phrase "short-circuiting" and I would've understood. Thanks. – John Roberts Oct 19 '12 at 19:04
  • 7
    @JohnRoberts well... that would defeat the purpose of the interview, no? :P – Luchian Grigore Oct 19 '12 at 19:05
  • 1
    @LuchianGrigore: wel... not necessarily. The "short circuiting" in logical ops is a *detail* someone not well accustomed to the language may not know. But once told, you can see his capability to reason about. Asked this way, it looks the question is more abot checking the knowelege of the syntax, than the capability to solve problems. Not by itself a bad thing, but at least it should be declared, not discovered. – Emilio Garavaglia Oct 19 '12 at 19:12
  • 1
    @EmilioGaravaglia oh, I agree. IMO this isn't a good interview questions. It's these types of small nit-picks that are not useful at all in evaluating someone. My point was that I think that asking this question is like asking "do you know what short-circuiting is?", only disguised in code. – Luchian Grigore Oct 19 '12 at 19:15
  • 5
    This is a poor interview question. And that code is pretty poor, as well. – John Dibling Oct 19 '12 at 19:19
  • @EmilioGaravaglia: Well, in C and C++, the short-circuiting behavior of `&&` and `||` is heavily used. For example `if (p && p->valid)` is only correct *because* `p->valid` is not evaluated when `p` is null. The question is poor, but knowing about the short-circuiting behavior is mandatory. – Matthieu M. Oct 19 '12 at 19:21
  • If you put "C/C++ expert" in your CV this would be a very good interview question, since everybody that took CS101 would know about "short circuiting", but recognizing it in code is a different story. !Otherwise – Andreas Oct 19 '12 at 19:54
  • 4
    @Andreas if you put "C/C++ expert" in your CV, you're clearly not. :D – Luchian Grigore Oct 19 '12 at 20:02
  • 1
    The interviewer absolutely shouldn't mention short-circuiting right away. If you try to answer the question and get the wrong answer, the interviewer should ask "do you know what operator short-circuiting is?" If that question makes you immediately return to the problem and get the right answer, you get partial credit. – Russell Borogove Oct 19 '12 at 21:59
2

The value of the expression ++i is 0 in this case, which is to say false so the and operation shortcuts and the latter expressions are never evaluated.

dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234