Блог пользователя theakhilarya

Автор theakhilarya, история, 3 года назад, По-английски

I want to compare four integers and see whether they are equal or not. So wrote the following,

int a = 1, b = 2, c = 3, d = 4;
if (a != b != c != d)
{
    //do something
}

This apparently shows no error. But, in fact, giving the wrong answer. Can someone explain this, please?

  • Проголосовать: нравится
  • -8
  • Проголосовать: не нравится

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

you have to compare each pair, there's no shortcut:

if (a != b && b != c && c != d) {
    // ...
}

or, you can also throw these variable in an std::set and check if the size is 4

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится +3 Проголосовать: не нравится

    also, your code compiles because it's equal to

    if (((a != b) != c) != d) {
        // ...
    }
    

    or something similar