468,504 Members | 1,928 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,504 developers. It's quick & easy.

Strange if condition

39
Hi all.I have a problem.

if (some condition)
printf("hello");
else
printf("world");

The "some condition" should be such that both "hello" and "world" are printed.Thanks for any help.

Regards,
Jerico
Sep 16 '06 #1
10 5314
sreema
3
i think "any condition" cannot be true and false at same time.



Hi all.I have a problem.

if (some condition)
printf("hello");
else
printf("world");

The "some condition" should be such that both "hello" and "world" are printed.Thanks for any help.

Regards,
Jerico
Sep 16 '06 #2
It is not possible in C. Then what is the need of if statement. Directly execute all the statements irrespective of the conditions. So C has switch case to handle this type of situations. Go through the switch case.
Sep 16 '06 #3
sreema
3
wht i meant was in C both if and else cannot execute sequentially. Definetly can be done using switch.



QUOTE=pukur123]It is not possible in C. Then what is the need of if statement. Directly execute all the statements irrespective of the conditions. So C has switch case to handle this type of situations. Go through the switch case.[/quote]
Sep 16 '06 #4
jerico
39
I also thought so.But then I remembered something called fork().If I use

if(!fork())
printf("hello");
else
printf("world");

then both "hello" and "world" are printed. Can there be any other solution?

Regards,
Jerico
Sep 16 '06 #5
I also thought so.But then I remembered something called fork().If I use

if(!fork())
printf("hello");
else
printf("world");

then both "hello" and "world" are printed. Can there be any other solution?

Regards,
Jerico
hey function which u used fork() is used in unix to create a child process.So here two threads of controls are generated one by parent and another by child.When fork is executed a child process is created.The parent process returns the processid of the child and the child process returns 0 .so one printf is executed by parent and another one by child .
Sep 16 '06 #6
There is always a solution to a problem.. It depends on how u think.. Instead of focussing on the problem, focus on the solution.. the question was not to try to figure out whether c supports the control entering if and else.. it just said the output needs to be hello world.. well answer is simple..


if(!printf("Hello"))
printf("Hello");
else
printf("World");

The conditions printf("Hello") will print hello and value retured is obviously true which by using ! can be made false so that it can then print world...

"Think, It may be a new experience"!!!
Jan 6 '07 #7
willakawill
1,646 1GB
There is always a solution to a problem.. It depends on how u think.. Instead of focussing on the problem, focus on the solution.. the question was not to try to figure out whether c supports the control entering if and else.. it just said the output needs to be hello world.. well answer is simple..


if(!printf("Hello"))
printf("Hello");
else
printf("World");

The conditions printf("Hello") will print hello and value retured is obviously true which by using ! can be made false so that it can then print world...

"Think, It may be a new experience"!!!
Nice answer :)
Jan 6 '07 #8
Very good question and answer. The more interesting way is presentation. If you have this kind of nice question please email me at vsagarmca@gmail.com
Any help would be appreciated.
Jul 11 '07 #9
Perhaps recursion might interest you..

Compliments of sleep+walker programming:1 Yahoo! Chat.

/* begin ifelse.c */

#include <stdio.h>

int main(void)
{
static int i;

if(!i++)
{
printf("Hello ");
main();
}
else
printf("World!");

return 0;
}

/* end ifelse.c */

C:\MinGW>gcc -Wall -pedantic -std=c99 -O2 -o ifelse.exe ifelse.c
C:\MinGW>ifelse
Hello World!
C:\MinGW>
Mar 23 '08 #10
I've considered KartikVlsi's idea and I'm quite convinced that unless by an act
of God the if statement will NEVER execute. My proposal eliminates writing code that will never execute, god willing.

I propose:

/* begin file.c */

#include <stdio.h>

int main(void)
{

if(!printf("Hello "))
{
}
else
printf("World!");

return 0;
}

/* end file.c */


C:\MinGW>gcc -Wall -pedantic -std=c99 -O2 -o file.exe file.c

C:\MinGW>file
Hello World!
C:\MinGW>
Mar 24 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Andrei Ivanov | last post: by
1 post views Thread by Sam Kong | last post: by
4 posts views Thread by Dovla | last post: by
6 posts views Thread by db2group88 | last post: by
1 post views Thread by pitjpz | last post: by
5 posts views Thread by A.Gallus | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.