The first type of expression came from C / C ++ , where it was possible to pass non-logical values โโto check conditions. For example. everything that was not 0 was true, and zero was false:
if (5) { } // true if (0) { } // false
Sometimes this created problems if you forgot to enter one '=' char:
if (x = 5) { } // this was true always and changed x value if (x == 5) { } // this was true, if x was equal to 5
So, Yoda syntax was used to get a compiler error if one '=' is missing:
if (5 = x) { } // this was generating compiler error for absent-minded programmers if (5 == x) { } // this was true, if x was equal to 5
C # allows only boolean value in conditions, So
if (x = 5) { }
What about boolean types?
if (y = true) { } if (y == true) { }
Well, this is useless code because you can just write if (y). Conclusion: Yoda syntax is missing with C / C ++, and you no longer need to use it.
Sergey Berezovskiy
source share