Level 1:
if(CONDITION) cout << "YES"; else cout << "NO"; cout << '\n';
Level 2:
cout << (CONDITION ? "YES" : "NO") << '\n';
Level 3:
This one just came to me while reading this amazing blog by HosseinYousefi.
cout << "NO\0YES" + 3 * CONDITION << '\n';
Maybe be also written as (thanks to Xellos for correction)
cout << &("NO\0YES"[3 * CONDITION]) << '\n';
With a bit of learning curve:
yeputons's one-liner:
puts(reinterpret_cast<const char*>(&(const int&)(218124110 - CONDITION * 01453204765)));
monsoon's one-liner:
cout << (char*)&(const int&)(23438671621410638LL >> (CONDITION << 5)) << '\n';
Level 4:
(by ShortestPath)
CONDITION && cout << "YES" || cout << "NO"; cout << '\n';
Please share other "YES-NO" one-liners you know. Will add it to this list.
is just so cringy.
Auto comment: topic has been updated by seul (previous revision, new revision, compare).
I think you meant in level 3 like this:
Otherwise if condition is true, it prints NO.
I updated the post, thank you for pointing it out. I forgot to make CONDITION variable common across all the levels.
cout << (cond ? YES : NO); ~~~
cout << ans[cond]; ~~~
theres also
if youre checking for universal quantification in a loop
that's a lot of line :)) the post is one-linear :))
you can do this:
;
thanks
nice modify!!!
Not C++ but wild enough
Assuming CONDITION is a variable:
cout << "NO\0YES\0"[3 * CONDITION] << endl;
doesn't work because[]
onconst char *
only gives you onechar
, i.e. 'N' or 'Y'. On the other hand, it can be replaced bycout << (3 * CONDITION)["NO\0YES\0"] << endl;
.Don't use
endl
though unless you need to flush.Corrected, thank you for pointing out.
What about this?
(CONDITION && cout << "Yes") || cout << "No";
In python:
cout << (cond ? YES : NO); ~~~
cout << ans[cond]; ~~~
output(cond); ~~~
output(cond); ~~~
output(cond); ~~~
struct foo { bool ans; foo() : ans(false) {} string output() { return ans ? "YES" : "NO"; } };
foo answer;
answer.output();class foo { public: bool ans; foo() : ans(false) {} void output() { cout << (ans ? "YES" : "NO"); } };
foo answer;
answer.output();output(answer); ~~~
output(answer); ~~~
OUTPUT(answer); ~~~
OUTPUT(answer); ~~~
output(answer); ~~~
A 2-item constant string array declared using a C++
#define
macro.puts(cond ? "YES" : "NO"); // adds new line at end
Ideone
Things lockdown making people do.
Well, I think this also works and most people will be messaging me mean things for writing this.
Great