4 ms·
(n > 0) && printf("0 ", 0); Never saw something like this. A conditional execution without an if?
by jhoechtl 5y ago
(n > 0) && printf("0 ", 0);
Never saw something like this. A conditional execution without an if?
- kmill 5y agoIn C, the && and || operators are short-circuiting. This works (in that it doesn't give a compiler error) because printf returns an int. I remember following a really old Python tutorial that suggested the way to write inline if/else statements was C and A or B which makes use of similar short-circuiting behavior. Nowadays this is written as A if C else B The and/or version depends on A being truthy, otherwise it evaluates both A and B, giving the value of B.
- qunabu 5y agothis is a classic functional programming that prefers expression like above instead of `if` statements.