tags:

views:

339

answers:

7

Possible Duplicate:
Stupid Question Regarding If-Else's Simultaneous Execution in C++ or C

Is it possible to put some condition, so that both if and else part in an if ...else control statement can be executed without any warning or error ??

+5  A: 

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

sepp2k
+8  A: 

No, there's no way to write a Schrödinger if clause.

You might be able to execute both with a goto, but it would never pass a code review.

John Franklin
+1 for Schrödinger :D
Sagar V
+1. Execute 0.4 of the first branch and 0.6 of the second one.
sharptooth
A: 

Maybe with goto ?

Benoit
+20  A: 

Do not use! ;-)

Yes, by forking.

if ( fork() ) {
    printf("if\n");
}
else {
    printf("else\n");
}

There are no real use cases to prefer the above code, unless it is for parallel execution.

Alan Haggai Alavi
Smart but this just confuses the issue.
Janek Bogucki
+1 Impressive ... but I don't know if it's a good or a bad impression :)
pmg
yughhhhhhhhhhhh
Gary Willoughby
+1 creative thinking; - a trillion because many people will implement it now ;-)
Mario The Spoon
It's an ingenious way to answer the question (so +1) but doesn't it make you feel dirty all over?
Basiclife
+1 for thinking outside the box; the question does not ask for a way to execute both branches *in the same process*.
Frerich Raabe
+1  A: 

I assume you're trying to have both branches of this sort of statement execute?

Dim X As Boolean
X = False
If X = True Then
    ...
Else
    ...
End If

You could get it to execute using GoTo ... but that goes against good programming practice.

Dim X As Boolean
X = False
If X = True Then
    ...
    Goto ElseStuff
Else
    ElseStuff:
    ...
End If

Instead of that you should write separate procedures / functions to accomplish the behavior you'd like to have execute in both statements ... or simply put the code which should execute in all cases outside of the If block.

That would be functionally equivalent to using the GoTo, plus it makes it clear to anybody else.

David T. Macknet
I would say that this goes against ... EVERYTHING ... not only goodp programming practice ;-)
Mario The Spoon
+3  A: 

Yes, it's possible:

#include <stdio.h>

#define else if (1)
int main(void)
{
    int test = 1;

    if (test == 1)
    {
        printf("if\n");
    }
    else
    {
        printf("else\n");
    }

    return 0;
}
#undef else

A note for newbies: Never do this in real life! Instead, think about your problem again...

What you probably wanted is :

#include <stdio.h>

int main(void)
{
    int one_condition = 1;
    int other_condition = 2;

    if ((one_condition == 1) || (other_condition == 2))
    {
        printf("if\n");
    }

    if ((one_condition != 1) || (other_condition == 2))
    {
        printf("quasi-else\n");
    }

    return 0;
}

You can replace the else-path by having another if-clause with negated conditions. This gives you the possibility to override it with a second condition.

groovingandi
+2  A: 

Maybe you've misunderstood your problem.

If you want a code block to execute regardless of the condition, take it out of the if...else statement.

void foofunc(int n)
{
     a = 44*n;
     if(a == 484)
     {
        //do something
     }
     else
     {
        //do something if a DOES NOT equal 484
     }

     //do something regardless of the outcome of the test.
}

In this example, ridiculous though it is, the last line is outside the condition statement, so will execute whether a == 484 or not, which seems to me to be the same as making c trigger your else block regardless of the if test succeeds.

Of course, else blocks are not mandatory, so if you don't care what happens if your condition fails, then simply don't have an else block.

void foofunc(int n)
{
     a = 44*n;
     if(a == 484)
     {
        //do something
     }

     //do something regardless of the outcome of the test.
}
Matt Ellen
+1 for the effort!
Mario The Spoon