开发者

Is it possible to execute both if and else part of an if --- else control statement? [duplicate]

开发者 https://www.devze.com 2023-01-18 03:32 出处:网络
This question already has answers here: Closed 12开发者_Go百科 years ago. Possible Duplicate: Simultaneous execution of both if and else blocks
This question already has answers here: Closed 12开发者_Go百科 years ago.

Possible Duplicate:

Simultaneous execution of both if and else blocks

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 ??


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.


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.


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.


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


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.
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消