Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing both 'If' as well as 'else' block [duplicate]

Tags:

c

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 both blocks by some condition?? Please help

like image 308
Prashant Avatar asked Oct 21 '10 14:10

Prashant


People also ask

Can you execute both the statements under if and else at the same time?

Use two if statements if both if statement conditions could be true at the same time. In this example, both conditions can be true. You can pass and do great at the same time. Use an if/else statement if the two conditions are mutually exclusive meaning if one condition is true the other condition must be false.

Is it possible that both the if part block and else part block are executed?

No, that is not possible (inside the same process).

How do you execute both IF and ELSE conditions in JavaScript?

The logical AND (&&) operator and if...else statements in JavaScript. In the logical AND ( && ) operator, if both conditions are true , then the if block will be executed. If one or both of the conditions are false , then the else block will be executed.


1 Answers

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.

like image 134
JeremyP Avatar answered Sep 23 '22 01:09

JeremyP