Why doesnt the OR logical operator( || ) work?

Bel*_*sem 0 c logical-operators

I tried to use the OR logical operator in a do-while statement but for some reason it wouldn't work.

它不使用OR逻辑运算符(仅使用一条语句),否则不起作用。

int main()
{
    char ansr;

    do
    {
        printf("What do you want to do?\n");
        printf("A = Add Employee\nR = Remove Employee\nE = Exit\n");
        scanf(" %c", &ansr);
    }while(ansr != 'E'||ansr != 'e');

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

每当我写“ E”或“ e”时,我都希望程序退出while循环,但由于某种原因,它将继续执行do-while语句。

Wil*_*son 5

逻辑OR(||)表示“如果其中任何一个为真”。在您的情况下,如果输入不是'E'或不是'e',则它将执行另一个迭代。这始终是正确的,因为即使是其中之一,也不会是另一个。

您可能正在考虑逻辑AND(&&):

while (ansr != 'E' && ansr != 'e');
Run Code Online (Sandbox Code Playgroud)

这意味着“如果这两个都是正确的”。如果ansr为'E'或'e',则子句之一将为false,然后整个表达式将为false。