Executing both 'If' as well as 'else' block [duplicate]
Possible Duplicates:
Stupid Question Regarding If-Else's Simultaneous Execution in C++ or C Is it possble to execute both if and else part of an if — else control statement ?
Hello everyone.. I had a question in an interview like this which i couldn't answer. Consider Following code block. Assume necessary header files.
if(.......)
{
printf("hello");
}
else
{
printf("world");
}
without moving/adding any code & without use of additional printing statements bring output as "Hello world"..You have to write the missing condition in if statement.. is it possible to execute 开发者_C百科both blocks by some condition?? Please help
This will work
if(schrodingers_cat_is_dead())
{
printf("hello");
}
else
{
printf("world");
}
Unfortunately, as soon as you look at the output, not only will it collapse into the state of only producing "hello" or "world", but there's a 50% chance you will have murdered a cat.
try fork()
! (and join afterwards). That's the only feature involving quantum mechanics that I know of.
fork()
returns two distinct values at a time, which only the Schrödinger's cat is capable of.
NOTE: In a comment, Donal Fellows suggests:
You'll get more reliable behavior with
!vfork()
If you can put in a macro definition as "part" of that condition...
if (1
#define else if (1)
)
{
printf("hello");
}
else
{
printf("world");
}
Then that code will indeed print "helloworld
". It's a horrible, dirty trick though; my soul is probably in trouble for mentioning it!
It depends on the exact phrasing of the question. As you describe it, the question does not ask you to execute both branches of the if statement. It asks you to insert a condition that results in printing "hello world" without changing anything else.
If you have complete freedom in what you put in the condition, you can use this solution:
if(printf("hello ") == -1)
{
printf("hello");
}
else
{
printf("world");
}
However, the solution uses "printf" in the condition, which is ruled out by one of the rules you gave. What's not clear to me is whether the printf prohibition also applies to what you write in the condition.
Note: Answer is edited to reflect the comments. The original answer ignored the prohibition on print statements.
There is one possible way, it is not recommended.
if(check==true)
{
printf("dont do this");
goto condition1;
}
else
{
condition1: ////this is crazy
printf("Dont do this ever");
}
You can, but it's probably bad practice. Seems the interviewer was testing your knowledge of the language. Built with gcc 4.1.2.
#include <stdio.h>
int main() {
if(!printf("hello ")) {
printf("hello");
} else {
printf("world");
}
return 0;
}
No, it's not possible. Either the first block or the else block will get executed; that is the nature of an if
statement.
There's no way to execute both branches of an if/else without exploiting it with weird things. If a condition is true then the else won't get executed and viceversa.
They're absolutely mutually exclusive.
Not without iterating over it again. I don't think you can do any Duff device like craziness in if statements. I suppose you could use goto, you say just by writing the condition.
In a debugger you could step through the if block, then move execution to the else block to execute, but that's just a silly answer to what seems to me to be a silly question.
The easiest way I think is this:
//if(.......)
{
printf("hello");
}
//else
{
printf("world");
}
Wouldn't this work?
if (!printf("hello")){
printf("hello");
}
else{
printf(" world");
}
you could try :
if()
{
printf("hello");
goto Found;
}
else
{
Found: printf("world");
}
精彩评论