Simultaneous execution of both if and else blocks

后端 未结 13 2348
难免孤独
难免孤独 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 17:52

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

提交回复
热议问题