-1

How is the value of b unchanged?

#include <iostream>
int main()
{
    int a = 5, b = 10;
    if (++a || ++b)
        std::cout << a << b;
    system("PAUSE");
    return 0;
}

The output is 610. But how?

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Tanmay Bhatnagar
  • 2,330
  • 4
  • 30
  • 50

1 Answers1

1

here's how the 'if' statement works:

if(condition1 || condition2 || condition 3){
//do this
}

now if condition1 is true (which in your code, it is since a!=0), the execution straightaway moves inside the block without checking 2 and 3.

If you wish to increment b as well, try && in place of ||

Saransh Kejriwal
  • 2,467
  • 5
  • 15
  • 39