Simultaneous execution of both if and else blocks

后端 未结 13 2338
难免孤独
难免孤独 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:05

    Here's a simple example:

    #include 
    
    int main() {
        int x;
        x = 6;
        if (x % 2 == 0) {
            printf("Divisible by two\n");
        }
        else if (x % 3 == 0) {
            printf("Divisible by three\n");
        }
        else {
            printf("Not divisible by two or three\n");
        }
        return 0;
    }
    

    Prints

    Divisible by two
    

    NOT

    Divisible by two
    Divisible by three
    

提交回复
热议问题