What is the <condition>?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

What is the <condition>??

What is the <condition> here so that the following code snippet prints both Hello and World... if(<condition>) printf("Hello"); else printf("World");

23rd Feb 2019, 11:17 AM
It's Me Apoo😬
It's Me Apoo😬 - avatar
4 Answers
+ 10
It's more of a hack. We know that no boolean value can be both true and false at the same time, so if print("Hello"): print("Hello") else: print("World") since None evaluates to False in Python. Same thing can be done in C: if (!printf("Hello")) printf("Hello"); else printf("World"); printf returns an integer value which will be evaluated to true. We negate that value to have it print "World".
23rd Feb 2019, 11:29 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
I think it's not possible.....because else will be true , if if becomes false or vice versa....both can't be true at the same time......
24th Feb 2019, 2:17 AM
Thanigai Velan
Thanigai Velan - avatar
+ 2
The condition could be anything that turns out to be "true" for example numbers that aren't 0, 12>5 etc.. C doesn't support boolean values,in other languages you could say "if (true)"....
23rd Feb 2019, 11:30 AM
Mensch
Mensch - avatar
+ 1
You can use fork() if(fork()) printf("hello") else printf("world") This is because fork() spawns a child process and fork() returns twice, once in the parent process, and once in the child process. In the parent process, it returns the process id of the child process, which is a positive integer, so the code in the if block is executed In the child process fork() returns 0, so the code in the else block gets executed in the child process.
25th Feb 2019, 10:43 AM
amogha0x00
amogha0x00 - avatar