By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Strange if condition

P: 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
Share this Question
Share on Google+
10 Replies


P: 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

P: 61
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

P: 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

P: 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

P: 11
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

P: 1
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

100+
P: 1,646
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

P: 1
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

P: 2
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

P: 2
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.