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

what is the Condition to be given to enter into both if and else loop ?

karthik baskar
P: 16
Expand|Select|Wrap|Line Numbers
  1. if(condition)
  2. {
  3.       printf("Hello");
  4. }
  5. else
  6. {
  7.       printf(" World");
  8. }
  9.  
What condition should I give to get the output as Hello World ??
Nov 25 '10 #1
Share this Question
Share on Google+
7 Replies


Banfa
Expert Mod 5K+
P: 8,916
The real answer to the question is that you do not want to associate yourself with someone asking such a stupid question.

On a *nix platform you could get both if and else branches to run by calling fork() as the condition although it would be a little random as to which branch ran first.
Nov 25 '10 #2

karthik baskar
P: 16
Actually this was a question asked in an interview. But, my friends said that this question has an answer other than what you have mentioned and it works in all versions of C/C++.
Nov 25 '10 #3

P: 5
I think !printf("hello ") will be worked.
Expand|Select|Wrap|Line Numbers
  1. if(!printf("hello"))
  2. {
  3.       printf("Hello");
  4. }
  5. else
  6. {
  7.       printf(" World");
  8. }
And fork() is also available.
Nov 25 '10 #4

Banfa
Expert Mod 5K+
P: 8,916
Using !printf("hello ") does not actually cause the if cause to run it just makes it look like it might have. For example

Expand|Select|Wrap|Line Numbers
  1. if(!printf("hello"))
  2. {
  3.       printf("Goodbye");
  4. }
  5. else
  6. {
  7.       printf(" World");
  8. }
  9.  
Still prints "HelloWorld" not "GoodbyeWorld" because the if clause does not actually run.


BTW I know its and interview question but that doesn't make it a good question just a silly one that anyone with any sense would know better than to ask.

If you have an option don't work for the people asking this question.
Nov 25 '10 #5

P: 7
if(getche() !== hello)
{
printf("Hello");
}
else
{
printf(" World");
}
Nov 25 '10 #6

P: 5
Sorry, I think you are not quite catch me.
if you want to print Goodbye, Then you should use the following code:
Expand|Select|Wrap|Line Numbers
  1. if(!printf("Goodbye"))
  2.       printf("Goodbye"); 
  3. else 
  4.       printf(" World"); 
  5.  
printf("GoodBye") would return an int.
Is that right?
Thank you!
Nov 26 '10 #7

Banfa
Expert Mod 5K+
P: 8,916
Sorry, I think you are not quite catch me.
No you are not catching me. The question is "what Condition can be use to make the flow of execution enter both the if and else code block".

This

Expand|Select|Wrap|Line Numbers
  1. if(!printf("Goodbye"))
  2.       printf("Goodbye"); 
  3. else 
  4.       printf(" World"); 
  5.  
Does not do it, the if code block is not entered the condition just produces output that makes it look like it might have done.

I demonstrated this by changing the output of the if code block to show that it does not run.

There is no condition that will make both code blocks run.
Nov 26 '10 #8

Post your reply

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