In C or C++
if ( x )
statement1;
else
statement2;
For what value of x
will both statements be executed?
I know
There is no single value for x
for which all paths of a conditional statement will be executed (which is kind of the point of a conditional statement; you want to execute one branch or the other based on x
).
However...
In C (and C++), you could use the setjmp
/longjmp
facility to execute both paths of an if
-else
:
#include <setjmp.h>
#include <stdio.h>
jmp_buf Env;
int main(void)
{
int status = setjmp(Env);
if (status == 0)
{
printf("In status == 0 branch\n");
longjmp(Env,1);
}
else
{
printf("In status != 0 branch\n");
}
return 0;
}
The initial call to setjmp
returns 0, so the first branch is taken. The call to longjmp
unwinds the stack back to the point where the setjmp
call returns, but this time the return value is 1 (the second argument to longjmp
), so the second branch is taken. However, this is not the same thing as status
evaluating to 0 and non-0 simultaneously.
In practice, it's similar to writing
for (status = 0; status < 2; status++)
{
if (status == 0)
printf("In status == 0 branch\n");
else
printf("In status != 0 branch\n");
}
although the semantics are different.
You could probably do something similarly ugly in C++ with exceptions, but I'm not enough of a C++ expert to say for sure.
You can use this:
#include <stdio.h>
int main()
{
if (//some condition//)
{
IF:{
printf("Hello "); //Code for if block.
}
goto ELSE;
}
else
{
goto IF;
ELSE:{
printf("world");//code for else block.
}
}
return 0;
}
Output: Hello world
for what value of x both statements will be executed??
Only in this case (on unix-like systems):
pid_t pid;
pid = fork();
if (pid == 0){
//some code
}
else {
//some code
}
In this case both branches will be always called simultaineously (well, more or less simultaneously), but in different processes.
I know we can execute if-else together like this:
This:
if(1){
goto ELSE;
}
else{
ELSE:
}
is a wrong construct. You need to use something like this instead:
if ( condition) {
//some code here
...
}
... //some other code here
If one branch is always called, then you don't need "else".
For single statement cases, only one of them will be executed, not both. This is the definition of if
.
HOWEVER, in the case of an if
statement using compound statements (a.k.a. statement block), the compiler may optimize the code to jump from the then
statements into duplicate statements in the else
block.
Example:
#include <iostream>
using namespace std;
int main(void)
{
static const char common_text1[] = "Some common text here.\n";
static const char common_text2[] = "Even more common code here.\n";
if (true)
{
cout << "Condition is true.\n";
cout << common_text1; // Execution may jump to same line below.
cout << common_text2;
}
else
{
cout << "\nCondition is false.\n";
cout << common_text1; // This line and the next may be executed when the
cout << common_text2; // condition is true.
}
return 0;
}
In the above example, the compiler may generate code so that when the condition is true
, the first statement in the true
block is executed, then execution jumps to the common statements in the else
block.
The compiler is rewriting the code:
if (true)
{
cout << "Condition is true.\n";
}
else
{
cout << "\nCondition is false.\n";
}
// The compiler factored-out the common statements.
cout << common_text1;
cout << common_text2;
This may happen when the compiler sees duplicate statements near the end of the statement block for both conditions.
for what value of x both statements will be executed?
There is no such value: either the value evaluates to true
(something != 0), or it evaluates to false
) (0). No other possible values exist.
I know we can execute if-else together like this: if(1){ goto ELSE; } else{ ELSE: }
That works but it isn’t depending of the value of the if
condition at all.
In a recursive function both branches can be executed:
void recursive(bool first)
{
if(first)
{
recursive(false);
}
else
{
//END
}
}
Invoking it with
recursive(true)
will execute the if branch followed by the else branch