1
int x = 1 , y = 1 , z = 1  ;

Now check these lines of code :-

cout << (++x || ++y)<<endl;     //Output 1
cout << x <<" " << y;          // now x = 2 and y = 1 . Why 'y' is not incremented ?

again values are initialized to 1

cout <<(++x && ++y )<<endl;     //Output 1
cout << x <<" " << y;         //now x = 2 and y = 2 . Why 'y' is incremented ?

again values are initialized to 1

cout << (++x ||++y && ++z )<<endl;  //Output 1
cout << x<<" "<< y<<" "<<z ;       //now x = 2 , y = 1 , z = 1.Why these outputs? 

Can some one explain me how compiler reads these codes ? I read about the Precedence order but I can't figure out how compiler works on these types of code.Even a Small help will be appreciated!

  • 5
    Look up "short-circuit evaluation". – EOF Jul 08 '16 at 12:59
  • Some additional [reading](http://en.cppreference.com/w/cpp/language/operator_logical). – James Adkison Jul 08 '16 at 13:02
  • duplicate of [Why does `++x || ++y && ++z` calculate `++x` first, even though operator `&&` has higher precedence than `||`](https://stackoverflow.com/q/3700352/995714) – phuclv Aug 18 '18 at 11:25

2 Answers2

3

It is called short circuiting.

  1. In your first case, since x has value of 1 on the left side, the right side of operator || is never called (because result would be true in any case in case with ||)- and hence y is never incremented.

  2. Similarly in your second example, since x is one on the left side, that means nothing for && - you still need to evaluate right side to see final result; if right hand side is false, then result is false, otherwise true. So in this case both left side and right side are evaluated. And values of x and y are increased.

  3. Again in your third case, due to short circuit, the right hand side involving y and z is never executed (because x has value of 1) - hence value of y and z is 1.

Here is some more info.

Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
-3

I'm not sure what you think happens when you type || and &&. These are both logical or/and operators.

The interesting thing about || and && is that their arguments are lazily evaluated. This is called short circuiting. In other words, as soon as the compiler have enough information to determine the expression's end result, it stops evaluating the arguments.

cout << (++x || ++y)<<endl;

++x is evaluated as the boolean true. Since with || it is enough for one side to be true for the entire expression to be true, ++y isn't evaluated.

cout <<(++x && ++y )<<endl;

++x is still true, so the compiler evaluates ++y as well, as if that turns out to be false, the expression would be false.

I'll leave the third one for you to figure out on your own.

I'll also add that this is a very strong feature. Without it, the following expression would dereference a null pointer:

if( a!=NULL && a->val>3 )...

The way it is, if a is null, then the right side isn't evaluated, and a isn't dereferenced.

Shachar Shemesh
  • 8,193
  • 6
  • 25
  • 57
  • 2
    Lazy evaluation is something completely different. – too honest for this site Jul 08 '16 at 13:07
  • 2
    This is called short circuit evaluation, not lazy. – NathanOliver Jul 08 '16 at 13:08
  • I've added the name of the feature, but please note that lazy evaluation is precisely what this is. The arguments are evaluated only if they are needed in order to determine the end result of the operator. Lazy evaluation, unlike short circuiting, is something non-C++ experts can understand. – Shachar Shemesh Jul 08 '16 at 13:12
  • Nope. Lazy evaluation: *In programming language theory, lazy evaluation, or call-by-need is an evaluation strategy which delays the evaluation of an expression until its value is needed* which is not what is happening. There is no delay going on. What is going on is that the compiler is smart enough to know it doesn't matter what the next condition is and skips it's evaluation completely. – NathanOliver Jul 08 '16 at 13:15
  • @NathanOliver, possibly skipping evaluation is a core part of lazy evaluation, and is precisely what is happening here. You seem to cast great importance to the question of compiler optimizing out stuff, but it is simply implementing the lazy evaluation. After all, if `x` is set to `-1`, `y` would get evaluated. – Shachar Shemesh Jul 08 '16 at 13:21
  • @NathanOliver, from the wikipedia entry for "lazy evaluation": *Short-circuit evaluation of Boolean control structures is sometimes called lazy.* – Shachar Shemesh Jul 08 '16 at 15:19
  • So right there you prove that is actually called short circuit and just some people cal it lazy. You should call it what it is. Calling it lazy is going to give the OP a bunch of stuff they do not care about if they search that term where as calling it short circuit will gives them exactly what they want. – NathanOliver Jul 08 '16 at 15:24
  • I did not call it lazy. I said that's what it is. You said it isn't. That is simply not true. – Shachar Shemesh Jul 08 '16 at 16:33