Simultaneous execution of both if and else blocks

后端 未结 13 2301
难免孤独
难免孤独 2020-12-10 17:45

In C or C++

if ( x )
    statement1;
else
    statement2;

For what value of x will both statements be executed?

I know

相关标签:
13条回答
  • 2020-12-10 18:14

    Without devious trickery, no, this is not possible. Consider what the expression means:

    if (cond) {
      ifTrue;
    } else {
      ifFalse;
    }
    

    This says to execute ifTrue if cond is true (a non-zero value/true), and to execute ifFalse if cond is false (zero/false). Since cond can't be simultaneously true and false, you can't execute both ifTrue and ifFalse without a special case, such as goto.

    0 讨论(0)
提交回复
热议问题