5

I came accross this expression, and can't understand the meaning of line 3 in the following snippet:

int A=0, B=0;
std::cout << A << B << "\n"; // Prints 0, 0
A += B++ == 0; // how does this exp work exactly? 
std::cout << A << B << "\n";  // Prints 1, 1

A adds B to it, and B is Post incremented by 1, what does the "==0" mean?

Edit: Here's the actual code:

int lengthOfLongestSubstringKDistinct(string s, int k) {
    int ctr[256] = {}, j = -1, distinct = 0, maxlen = 0;
    for (int i=0; i<s.size(); ++i) {
        distinct += ctr[s[i]]++ == 0; // 
        while (distinct > k)
            distinct -= --ctr[s[++j]] == 0;
        maxlen = max(maxlen, i - j);
    }
    return maxlen;
}
Batwoman05
  • 215
  • 1
  • 2
  • 9
  • 7
    Means your CS teacher is teaching you the wrong things the wrong way. Supplement your formal education with these [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Ron May 04 '18 at 12:42
  • It means compare the last value - the `B++` result - against 0 and return a boolean true / false. But this is fairly silly code. – Rup May 04 '18 at 12:43
  • 5
    This is code that if you wrote it in a job situation you should be immediately fired. – drescherjm May 04 '18 at 12:44
  • Read about c++ operators and their execution order. This example is confusing but unambigous if you know how this operators work. – Denis Sheremet May 04 '18 at 12:45
  • 1
    `<< <<` doesn't look like valid C++. Did you mean `<< " " <<`? – Lundin May 04 '18 at 12:51
  • @Lundin Yes that was a typo. Corrected it now. Thanks. – Batwoman05 May 04 '18 at 12:54
  • Though I'm surprised that C++ allows arithemtic with `bool`. I can see how this code would compile clean in C, but g++ with C++14 lets it through as well. – Lundin May 04 '18 at 12:54
  • 1
    @Lundin bool will just get promoted to int; in the same way int will be promoted to long etc. – UKMonkey May 04 '18 at 12:56
  • @UKMonkey Oh right. Silent promotion from `bool` to `int`. Eww. That's even worse than "int booleans" in C. Just eww. – Lundin May 04 '18 at 13:01

2 Answers2

7
B++ == 0 

This is a boolean expression resulting in true or false. In this case the result is true, true is then added to A. The value of true is 1 so the (rough) equivalent would be:

if(B == 0)
  A += 1;
++B;

Note that this isn't particulary good or clear to read code and the person who wrote this should be thrown into the Gulags.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
  • 2
    Maybe you should precise that B is beeing incremented after the comparaison, which explains why B++ == 0 is true – souki May 04 '18 at 12:46
  • 1
    @souki Added a rough equivalent to make it easier to understand. – Hatted Rooster May 04 '18 at 12:46
  • So when this line is executed, it checks if B==0, adds 1 to A if true, then increments B? – Batwoman05 May 04 '18 at 12:48
  • 2
    have an upvote for suggesting a punishment for the person who wrote this. – UKMonkey May 04 '18 at 12:50
  • 3
    @Batwoman05 some people define a good programmer as one who can get something done in as few lines as possible. Some people produce rubbish that no one else can understand; including themselves in a week; even if it works. Others define a good programmer who can produce code that any other programmer can pick up and maintain. Sure, which you pick is personal opinion .... but if you pick the first you're wrong ;) – UKMonkey May 04 '18 at 12:54
  • 3
    Or a more cruel punishment yet: have them maintain the code. – Lundin May 04 '18 at 12:56
1

Lets break this expression into pieces: A += value, whereas value = B++ == 0. As later cout suggests, value == 1. Why is that? Here is why: value is result of comparison of B++ and 0, but ++ (increment) operation, when written after operand, is being processed after the comparison, i.e. if you write A += ++B == 0 the later cout should (and does) print 0, 1.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Binpord
  • 90
  • 8