为什么在if-else中使用运算符OR(逻辑运算符),为什么不在if-else中使用AND(逻辑运算符)

时间:2019-08-28 02:18:49

标签: c

我使字母表看起来像是使用星号来构成字母表,但是我对为什么在if-else上使用OR运算而不是AND运算感到困惑?

int row,column;
for(row=1;row<=4;row++){
    for(column=1;column<=7;column++){
        if((column==1 || column==2 || column==3 || column==5 || column==6 || column==7) && (row==1))
            printf(" ");
        else if((column==1 || column==2 || column==4 || column==6 || column==7) && (row==2))
            printf(" ");
        else if((column==1 || column==7) && (row==3))
            printf(" ");
        else if((column==2 || column==3 || column==4 || column==5 || column==6) && (row==4))
            printf(" ");
        else
            printf("*");
    } printf("\n");
}

为什么使用运算符||在if-else中吗?为什么不在条件列的if-else中使用运算符&& ...

1 个答案:

答案 0 :(得分:1)

  • column==1 || column==2column1column2时为true
  • column==1 && column==2column1column时,
  • 2将为true。这永远不会发生,因为变量不能同时为12,因此此条件将始终为false。