C++: Ways of printing "Yes" and "No" answers

Revision en16, by seul, 2020-04-14 21:56:31

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.

Tags c++, tips-and-tricks

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en16 English seul 2020-04-14 21:56:31 36
en15 English seul 2020-04-05 19:05:54 16
en14 English seul 2020-04-05 16:56:19 326 Tiny change: 'g curve:\n[user:ye' -> 'g curve:\n\n[user:ye'
en13 English seul 2020-04-05 09:25:57 207 Tiny change: 'Level 4:**\n\n(by [user:' -> 'Level 4:** (by [user:'
en12 English seul 2020-04-05 00:49:12 55
en11 English seul 2020-04-05 00:47:41 65 Reverted to en9
en10 English seul 2020-04-05 00:35:19 65
en9 English seul 2020-04-05 00:26:36 4
en8 English seul 2020-04-05 00:22:24 120
en7 English seul 2020-04-05 00:19:39 122 Tiny change: ';\n~~~~~\n[user:ch' -> ';\n~~~~~\n\n### **Level 4:**\n\n[user:ch'
en6 English seul 2020-04-04 22:02:19 20
en5 English seul 2020-04-04 19:26:45 88
en4 English seul 2020-04-04 19:01:30 60
en3 English seul 2020-04-04 18:36:11 86 (published)
en2 English seul 2020-04-04 18:32:20 24
en1 English seul 2020-04-04 18:29:09 481 Initial revision (saved to drafts)