<p>Would it be possible to print <code>Hello</code> twice using single <code>condition</code>?</p>

<pre><code>if  "condition"
  printf ("Hello");
else
  printf("World");
</code></pre>

[{'answer_id': 33202, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 3, 'selected': False, 'text': '<p>The <code>if</code> statement executes one or the other of the controlled statements (both <code>printf</code> in your example). No matter what you use for <code>condition</code>, that snippet will either print "Hello", or "World", but never both.</p>\n\n<p>Edit: Okay, so it\'s a trick question and you can put whatever you like in the condition (including a call to an entire other function that does anything you want). But that\'s hardly interesting. I can\'t believe I got downmodded for giving a correct answer.</p>\n'}, {'answer_id': 33205, 'author': 'scoob', 'author_id': 1165587, 'author_profile': 'https://Stackoverflow.com/users/1165587', 'pm_score': 3, 'selected': False, 'text': '<pre><code>if ( printf("Hello")==0)\n</code></pre>\n\n<p>see\n[<a href="http://www.coders2020.com/what-does-printf-return]" rel="nofollow noreferrer">http://www.coders2020.com/what-does-printf-return]</a></p>\n\n<p>(matt corrected my =, thanks, C is far away)</p>\n'}, {'answer_id': 33206, 'author': 'Jake Pearson', 'author_id': 632, 'author_profile': 'https://Stackoverflow.com/users/632', 'pm_score': 0, 'selected': False, 'text': '<pre><code>if(printf("Hello") == 1)\n    printf("Hello")\nelse\n    printf("World")\n</code></pre>\n'}, {'answer_id': 33208, 'author': 'Matt Dillard', 'author_id': 863, 'author_profile': 'https://Stackoverflow.com/users/863', 'pm_score': 7, 'selected': True, 'text': '<pre><code>if ( printf("Hello") == 0 )\n    printf ("Hello");\nelse\n    printf ("World");\n</code></pre>\n\n<p>:-)</p>\n'}, {'answer_id': 33211, 'author': 'travis', 'author_id': 1414, 'author_profile': 'https://Stackoverflow.com/users/1414', 'pm_score': 2, 'selected': False, 'text': '<pre><code>if  (true) printf ("Hello"); if (false)\n    printf ("Hello");\nelse\n    printf("World");\n</code></pre>\n'}, {'answer_id': 33212, 'author': 'Frank Krueger', 'author_id': 338, 'author_profile': 'https://Stackoverflow.com/users/338', 'pm_score': 4, 'selected': False, 'text': '<pre><code>"condition" === (printf("Hello"), 0)\n</code></pre>\n\n<p>Really lame:</p>\n\n<pre><code>int main() {\n    if  (printf("Hello"), 0)\n        printf ("Hello");\n    else\n        printf("World");\n}\n</code></pre>\n\n<p>I prefer the use of the comma operator because you don\'t have to look up the return value of <code>printf</code> in order to know what the conditional does. This increases readability and maintainability. :-)</p>\n'}, {'answer_id': 33213, 'author': 'Daren Thomas', 'author_id': 2260, 'author_profile': 'https://Stackoverflow.com/users/2260', 'pm_score': 1, 'selected': False, 'text': '<p>This could work:</p>\n\n<pre><code>if (printf("Hello") - strlen("Hello"))\n    printf("Hello")\nelse\n    printf("World")\n</code></pre>\n\n<p>This snippet emphasizes the return value of <code>printf</code>: The number of characters printed.</p>\n'}, {'answer_id': 33216, 'author': 'Billy Jo', 'author_id': 3447, 'author_profile': 'https://Stackoverflow.com/users/3447', 'pm_score': 0, 'selected': False, 'text': '<pre><code>if (printf("Hello") &lt; 1)\n    printf("Hello");\nelse\n    printf("World");\n</code></pre>\n'}, {'answer_id': 33219, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<pre><code>#define CONDITION (0) if (0) {} else\n</code></pre>\n\n<p>or some such.</p>\n\n<p>If you see such a question on an interview, run away as fast as you can! The team that asks such questions is bound to be unhealthy.</p>\n\n<p>Edit - I forgot to clarify - this relies on "else" being matched with closest open "if", and on the fact that it\'s written as "if CONDITION" rather than if (CONDITION) - parenthesis would make the puzzle unsolvable.</p>\n'}, {'answer_id': 33230, 'author': 'infralite', 'author_id': 3423, 'author_profile': 'https://Stackoverflow.com/users/3423', 'pm_score': 0, 'selected': False, 'text': '<blockquote>\n  <p>Greg wrote:</p>\n  \n  <blockquote>\n    <p>No matter what you use for condition, that snippet will either print "Hello", or "World", but never both.</p>\n  </blockquote>\n</blockquote>\n\n<p>Well, this isn\'t true, but why you would <em>want</em> it to print both, I can\'t find a use case for. It\'s defeating the point of having an if statement. The likely "real" solution is to not use an if at all. Silly interview questions... :)</p>\n'}, {'answer_id': 33253, 'author': 'ryan', 'author_id': 2454, 'author_profile': 'https://Stackoverflow.com/users/2454', 'pm_score': 0, 'selected': False, 'text': '<p>Very interesting guys, thanks for the answers. I never would have thought about putting the print statement inside the if condition.</p>\n\n<p>Here\'s the Java equivalent:</p>\n\n<pre><code>    if ( System.out.printf("Hello").equals("") )\n        System.out.printf("Hello");\n    else\n        System.out.printf("World");\n</code></pre>\n'}, {'answer_id': 33724, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>Without knowing the return value of <code>printf</code> off the top of your head:</p>\n\n<pre><code>if (printf("Hello") &amp;&amp; 0)\n    printf("Hello");\nelse\n    printf("World");\n</code></pre>\n'}, {'answer_id': 80684, 'author': 'GowriKumar', 'author_id': 15150, 'author_profile': 'https://Stackoverflow.com/users/15150', 'pm_score': 4, 'selected': False, 'text': '<p>If it is on Unix:</p>\n\n<pre><code>if  (fork())\n    printf ("Hello");\nelse\n    printf("World");\n</code></pre>\n\n<p>Ofcoures that doesn\'t guarantee the order 0f the prints</p>\n'}, {'answer_id': 784683, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>Solution 1:</p>\n\n<pre><code>int main(int argc, char* argv[])\n{   \n    if( argc == 2 || main( 2, NULL ) )\n    {\n        printf("Hello ");   \n    }\n    else\n    {\n        printf("World\\n");\n    }\n    return 0;\n}\n</code></pre>\n\n<p>Solution 2 (Only for Unix and Linux):</p>\n\n<pre><code>int main(int argc, char* argv[])\n{   \n    if( !fork() )\n    {\n        printf("Hello ");   \n    }\n    else\n    {\n        printf("World\\n");\n    }\n    return 0;\n}\n</code></pre>\n'}, {'answer_id': 1373971, 'author': 'Johannes Schaub - litb', 'author_id': 34509, 'author_profile': 'https://Stackoverflow.com/users/34509', 'pm_score': 2, 'selected': False, 'text': '<p>No love for <code>exit</code>?</p>\n\n<pre><code>if(printf("HelloWorld"), exit(0), "ByeBye") \n    printf ("Hello");\nelse\n    printf ("World");\n</code></pre>\n'}, {'answer_id': 1774682, 'author': 'Adriaan Stander', 'author_id': 144424, 'author_profile': 'https://Stackoverflow.com/users/144424', 'pm_score': 0, 'selected': False, 'text': '<p>Dont use an if else block then.</p>\n\n<p>EDIT to Comment.</p>\n\n<p>It might then mean that the code be in both blocks, or before/after the block if it is required to run in both cases.</p>\n'}, {'answer_id': 1774683, 'author': 'Ed S.', 'author_id': 1053, 'author_profile': 'https://Stackoverflow.com/users/1053', 'pm_score': 2, 'selected': False, 'text': '<p>So... you want to execute the code inside the if block... and the code inside of the else block... of the same if/else statement?  Then... you should get rid of the else and stick taht code in the if.</p>\n\n<pre><code>if something\n  do_this\n  do_that\nend\n</code></pre>\n\n<p>The else statement is designed to execute only if the if statement is not executed and vice-versa, that is the whole point.  This is an odd question...</p>\n'}, {'answer_id': 1774688, 'author': 'Michael Stum', 'author_id': 91, 'author_profile': 'https://Stackoverflow.com/users/91', 'pm_score': 1, 'selected': False, 'text': '<p>Just put the code before or after the if..else block.</p>\n\n<p>Alternatively, if you have an "if, else if, else" block where you want to execute code in some (but not all) branches, just put it in a separate function and call that function within each block.</p>\n'}, {'answer_id': 1774706, 'author': 'Tordek', 'author_id': 77542, 'author_profile': 'https://Stackoverflow.com/users/77542', 'pm_score': 2, 'selected': False, 'text': '<p>Comment the "else" ;)</p>\n\n<pre><code>if(foo)\n{\n    bar();\n}\n//else\n{\n    baz();\n}\n</code></pre>\n'}, {'answer_id': 1774711, 'author': 'Eli Bendersky', 'author_id': 8206, 'author_profile': 'https://Stackoverflow.com/users/8206', 'pm_score': 4, 'selected': False, 'text': '<p>Buckle your seatbelts:</p>\n\n<pre><code>#include &lt;stdio.h&gt;\n#include &lt;setjmp.h&gt;\n\nint main()\n{\n    jmp_buf env;\n\n    if (!setjmp(env))\n    {\n        printf("if executed\\n");\n        longjmp(env, 1);\n    }\n    else\n    {\n        printf("else executed\\n");\n    }\n\n    return 0;\n}\n</code></pre>\n\n<p>Prints:</p>\n\n<pre><code>if executed\nelse executed\n</code></pre>\n\n<p>Is this what you mean? I doubt it, but at least it\'s possible. Using <code>fork</code> you can do it also, but the branches will run in different processes.</p>\n'}, {'answer_id': 1774717, 'author': 'sud03r', 'author_id': 91593, 'author_profile': 'https://Stackoverflow.com/users/91593', 'pm_score': 4, 'selected': False, 'text': '<p>This sounds to me like some interview puzzle. I hope this is close to what you want.</p>\n\n<pre><code>\n#include &lt;stdio.h&gt;\n\nint main()\n{\n static int i = 0 ;\n if( i++==0 ? main(): 1)\n  printf("Hello,");\n else\n  printf("World\\n");\n\n return 0 ;\n}\n</code></pre>\n\n<p>prints <code>Hello, World</code></p>\n'}, {'answer_id': 1774727, 'author': 'rtpg', 'author_id': 122757, 'author_profile': 'https://Stackoverflow.com/users/122757', 'pm_score': 0, 'selected': False, 'text': '<p>use a goto, one of the single most underused keywords of our day</p>\n'}, {'answer_id': 1774757, 'author': 'Jonathan Leffler', 'author_id': 15168, 'author_profile': 'https://Stackoverflow.com/users/15168', 'pm_score': 2, 'selected': False, 'text': "<p>The basic answer is that in the ordinary course of events you neither want to execute both the statements in the 'if' block and the 'else' block in a single pass through the code (why bother with the condition if you do) nor can you execute both sets of statements without jumping through grotesque hoops.</p>\n\n<p>Some grotesque hoops - evil code!</p>\n\n<pre><code>    if (condition == true)\n    {\n         ...stuff...\n         goto Else;\n    }\n    else\n    {\nElse:\n        ...more stuff...\n    }\n</code></pre>\n\n<p>Of course, it is a plain abuse of (any) language because it is equivalent to:</p>\n\n<pre><code>    if (condition == true)\n    {\n         ...stuff...\n    }\n    ...more stuff...\n</code></pre>\n\n<p>However, it might achieve what the question is asking.  If you have to execute both blocks whether the condition is true or false, then things get a bit trickier.</p>\n\n<pre><code>    done_then = false;\n    if (condition == true)\n    {\nThen:\n         ...stuff...\n         done_then = true;\n         goto Else;\n    }\n    else\n    {\nElse:\n        ...more stuff...\n        if (!done_then) goto Then;\n    }\n</code></pre>\n"}, {'answer_id': 1774759, 'author': 'Dustin E', 'author_id': 215931, 'author_profile': 'https://Stackoverflow.com/users/215931', 'pm_score': 2, 'selected': False, 'text': '<pre><code>int main()\n{\n    runIfElse(true);\n    runIfElse(false);\n\n    return 0;\n}\n\nvoid runIfElse(bool p)\n{\n    if(p)\n    {\n     // do if\n    }\n    else\n    {\n     // do else\n    }\n}\n</code></pre>\n'}, {'answer_id': 1774788, 'author': 'lpz', 'author_id': 215987, 'author_profile': 'https://Stackoverflow.com/users/215987', 'pm_score': 0, 'selected': False, 'text': "<p>Cheeting with an empty else statement:</p>\n\n<pre><code>if (condition)\n    // do if stuff\nelse;\n    // do else stuff\n</code></pre>\n\n<p>If you don't like the fact that else; is actually an empty else statement try this:</p>\n\n<pre><code>for (int ii=0; ii&lt;2; ii++)\n{\n    if (condition &amp;&amp; !ii)\n        // do if stuff\n    else\n    {\n        // do else stuff\n        break;\n    }\n}\n</code></pre>\n"}, {'answer_id': 10151648, 'author': 'firesofmay', 'author_id': 679390, 'author_profile': 'https://Stackoverflow.com/users/679390', 'pm_score': 0, 'selected': False, 'text': '<p>Two possible Solutions without using printf statements :-</p>\n\n<p>First :-</p>\n\n<pre><code>#include &lt;stdio.h&gt;\n\nint\nmain(void)\n{\n  if (!stdin || (stdin = 0, main()))\n    printf("hello");\n  else\n    printf("world");\n  return 0;\n}\n</code></pre>\n\n<p>Second</p>\n\n<pre><code>#include&lt;stdio.h&gt;\nvoid main()\n{\nif (1\n#define else if (1) \n)\n{ \n  printf("hello"); \n} \nelse\n { \n    printf("world"); \n}\n}\n</code></pre>\n\n<p>Reference :- <a href="http://ideone.com/q5Y7j" rel="nofollow">Link1</a> , <a href="http://codepad.org/qfS5my7p" rel="nofollow">Link2</a></p>\n'}, {'answer_id': 12060150, 'author': 'mafia', 'author_id': 1137405, 'author_profile': 'https://Stackoverflow.com/users/1137405', 'pm_score': -1, 'selected': False, 'text': '<p>The condition to this question is:</p>\n\n<pre><code> if(printf("hello")? 0 : 1) {   }\n</code></pre>\n'}, {'answer_id': 25564384, 'author': 'shiju', 'author_id': 1179556, 'author_profile': 'https://Stackoverflow.com/users/1179556', 'pm_score': 0, 'selected': False, 'text': '<pre><code>if (printf("hello") &amp; 0)\n{\nprintf("hello");\n}\nelse\n{\nprintf("world");\n</code></pre>\n\n<p>No need to bother about the return value of printf.</p>\n'}, {'answer_id': 29992356, 'author': 'Shankha Jana', 'author_id': 2550691, 'author_profile': 'https://Stackoverflow.com/users/2550691', 'pm_score': 1, 'selected': False, 'text': '<pre><code> #include&lt;stdio.h&gt;\n  int main()\n{\n if(! printf("Hello"))\n     printf ("Hello");\nelse\n    printf ("World");\n return 0;\n}\n</code></pre>\n\n<p>Because Printf returns the number of character it has printed successfully. </p>\n'}, {'answer_id': 29992672, 'author': 'Michael Dorgan', 'author_id': 527574, 'author_profile': 'https://Stackoverflow.com/users/527574', 'pm_score': 0, 'selected': False, 'text': '<p>Abuse of preprocessing - with cleanup at least.</p>\n\n<pre><code>\n#define else \nif(1)\n{\n   printf("hello");\n}\nelse\n{\n   printf("world");\n}\n#undef else\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/33199', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2454/']