mardi 29 octobre 2019

How do you interpret this code correctly step by step? (New to programming)

My problem is that I dont know how the compiler runs through the different if statements correctly and why he skips some in this case.

I tried checking if the conditions from start to bottom are true or false and thus find the correct output of the program. But why doesnt the program output 84 here:

if (a > c) cout << 84;
else cout << 48

Full Program:

int main()
{
  constexpr int a{8};
  constexpr int b{4};
  constexpr int c{1};

  if (a < b < c)
    if (c > b > a)
      if (a > c) cout << 84;
      else cout << 48;
    else
      if (b < c) cout << 14;
      else cout << 41;
  else
    if (b < a < c)
      if (a < c) cout << 81;
      else cout << 18;
    else
      if (b < c) cout << 17;
      else cout << 71;

  return 0;
}

The program outputs only 41. Why?

Aucun commentaire:

Enregistrer un commentaire