-2

This is My Code:

int main()
{
    int i=2,j=2,c=2,d;

    d= ++i || ++j && c;
    printf("I is %d\n",i);
    printf("J is %d\n",j);
    printf("C is %d\n",c);
    printf("D is %d\n",d);
    return 0;
}

The output of the following code is:

i is 3                                                                                                                                                                          
j is 2                                                                                                                                                                          
c is 2                                                                                                                                                                          
d is 1

Now my question is, if ++i is 3 then why ++j is 2?

What is the difference between ++i & i++? Also I want to know that how come d is 1 ?

TryinHard
  • 4,078
  • 3
  • 28
  • 54
  • why is value of J still 2 after ++j? – user27654321 May 02 '15 at 05:18
  • 5
    note that `++j` is never executed. there must be some question about short-circuiting and logical operators already – Diego May 02 '15 at 05:18
  • 4
    Well, the duplicate everyone is voting for certainly answers the *title* of this question. Too bad the *question* doesn't really match its own title (or the duplicate-selection). – WhozCraig May 02 '15 at 05:24

3 Answers3

7

You asked:

Now my question is if ++i is 3 then why ++j is 2?

++j is never executed since ++i evaluates to true.

The expression ++i || ++j && c is equivalent to ++i || (++j && c) due to operator precedence.

The run time evaluates the expression ++i || ++j && c from left to right.
The first term, ++i evaluates to 3, with the side effect that the value of i is 3. Since it is non-zero, the run time does not need to evaluate anything after that. Since ++j is never evaluated, the value of j is not changed.

What is the difference between ++i & i++?

The value of the expression ++i is i+1 with the side effect that the value of i is incremented by 1.

The value of the expression i++ is i with the side effect that the value of i is incremented by 1.

int i = 2;
int x = ++i;  // x == 3, i == 3 at the end of execution of the statement
int y = i++;  // y == 3, i == 4 at the end of execution of the statement

I want to know that howcome D is 1

The value of d is set to 1 since the boolean value of the expression is true.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
5

++i is pre increment.
i++ is post increment.
Suppose here i = 2

In Post In crement

printf("%d",i++); // it will print output 2 then increment value of i to 3

In Pre Increment

printf("%d",++i); //first it will increment value of i to 3, then print output 3

d= ++i || ++j && c;    

Condition will be performed in this manner ++i || (++j && c);
As i=2, after ++i it will become 3.
In OR if first condition is true it will skip second condition. means ++j && c will not be performed.
Result of ++i || ++j && c is 1
so d = 1.


printf("I is %d\n",i);  // i = 3;
printf("J is %d\n",j);  // j = 2;
printf("C is %d\n",c);  // c = 2;
printf("D is %d\n",d);  // d = 1;  

Short Circuit evaluation

Himanshu
  • 4,327
  • 16
  • 31
  • 39
  • 1
    `&&` has higher precedence than `||`. So it will first solve `++i`, and then if that is false, solve `++j && c` , and finally solve those two things `||`'d together. – M.M May 02 '15 at 05:31
  • @MattMcNabb , If the expression is `++i || (++j && c)`, then isn't `(++j && c)` supposed to be executed first as they are in parenthesis? – Spikatrix May 02 '15 at 05:40
  • @CoolGuy no, parentheses has nothing to do with order of evaluation – M.M May 02 '15 at 05:50
0

Reason for the evaluation is short-circuiting of the boolean operators && and ||.

  • For &&, if the left-hand side expression is false, the combined result is false (the right-hand side expression is never evaluated).
  • For ||, if the left-hand side expression is true, the combined result is true (the right-hand side expression is never evaluated).

This is mostly important when the right-hand expression has side effects, such as altering values(or calling some functions with side-effects), for example in your case:

d= ++i || ++j && c;

Note that the combined conditional expression would increment i by 1 since the conditional expression on the left of || is true, the conditional expression on the right-hand side i.e. ++j && c is never evaluated.

Thus, the total expression will lead eventually to:

d = 1 || ++j && c;

which explains why d=1?

Now if you change the value of i=-1 then the expression on the right of the || will be executed and the j will be incremented as per your expectation

Now for the performance effects of i++ vs ++i check here :)

Community
  • 1
  • 1
TryinHard
  • 4,078
  • 3
  • 28
  • 54