【问题标题】:||, && operators in C||, && C 中的运算符
【发布时间】:2017-05-11 19:29:21
【问题描述】:

为什么此代码仅适用于 && 运算符?

我认为应该是||,但我错了。 Choice不能同时等于2个值?

我需要询问用户的输入,直到选择等于'a''d',但为什么我需要写&&?没看懂。

do
{
    scanf("%c", &choice);
} while ( choice != 'a' && choice != 'd' );

我想使用||,但它不起作用。

【问题讨论】:

  • 检查德摩根定律 here.

标签: c logical-operators logical-or logical-and


【解决方案1】:

您可以使用||。你需要改变while循环条件表达式如下

while ( !(choice == 'a' || choice == 'd') ); 

只是DeMorgan's Law的一个应用,怎么不影响AND&OR

______   ___  ___
A || B =  A && B
______   ___  ___
A && B =  A || B

记住它是:“打破界限,改变标志”。

INPUT   OUTPUT 1        OUTPUT 2
A   B   NOT (A OR B)    (NOT A) AND (NOT B)
0   0          1                 1
0   1          0                 0
1   0          0                 0
1   1          0                 0

INPUT   OUTPUT 1        OUTPUT 2
A   B   NOT (A AND B)   (NOT A) OR (NOT B)
0   0           1               1
0   1           1               1
1   0           1               1
1   1           0               0

【讨论】:

    【解决方案2】:

    我需要询问用户的输入,直到选择等于“a”或“d”

    这个条件可以写成这样

    choice == 'a' ||  choice == 'd'
    

    因此,如果您希望循环迭代直到此条件为真,您应该编写

    do
    {
        //...
    } while ( !( choice == 'a' ||  choice == 'd' ) );
    

    或者是否包含标题

    #include <iso646.h> 
    

    那你就可以写了

    do
    {
        //...
    } while ( not ( choice == 'a' ||  choice == 'd' ) );
    

    甚至喜欢

    do
    {
        //...
    } while ( not ( choice == 'a' or  choice == 'd' ) );
    

    条件

    !( choice == 'a' ||  choice == 'd' )
    

    not ( choice == 'a' or  choice == 'd' )
    

    等价于

    !( choice == 'a' ) &&  !( choice == 'd' )
    

    not ( choice == 'a' ) and  not ( choice == 'd' )
    

    这又相当于

    ( choice != 'a' ) &&  ( choice != 'd' )
    

    括号可以省略,这样你就有了

    do
    {
        //...
    } while ( choice != 'a' &&  choice != 'd' );
    

    【讨论】:

    • 谢谢弗拉德很好的解释!我想我明白了。
    【解决方案3】:

    你可以用这个替换条件:

    while (!(choice == 'a' || choice == 'd'));
    

    使用此条件 do-while 语句将一直执行,直到选项等于 'a' 或 'd'。

    【讨论】:

    • 这只适用于:while ( !(choice == 'a' || choice == 'd') );
    • 需要另一对括号
    【解决方案4】:

    操作符的工作原理没有问题,你需要在这里获取代码的逻辑。

    首先,只要while 中的条件为 TRUE,do...while 循环就会运行。

    在您的情况下,您想询问用户的输入,直到选择等于 'a''d'

    • 所以,换句话说,只要用户输入不等于ad,就需要循环。

    • 逻辑上,如果输入不等于a,它仍然可以等于d,所以你必须检查这两种情况。只有当ad 没有 是输入时,才继续循环。

    请记住,您不是在检查相等性,而是在检查不等式。仅当两个不等式都满足时,只有 while 条件的计算结果为 TRUE_ 并且您继续循环以请求新值。

    简而言之,read DeMorgan's laws

    【讨论】:

      【解决方案5】:

      如果你想使用 or (||) 运算符,你必须应用 De Morgan's law:

      假设 a 和 b 是布尔值,所以

      a || b <=> !a && !b
      
      a && b <=> !a || !b
      

      这个,

      while ( choice != 'a' && choice != 'd' );
      

      相同
      while !( choice == 'a' || choice == 'd' );
      

      【讨论】:

        【解决方案6】:

        嘿,它不起作用,因为你希望它不等于 a 和 b。

        not equal to a and not equal to b
        
        so it will be !( eqaul to a or eqaul to b) (De morgan's)
        

        德摩根定律说

        not( a and b)  = not (a) or not(b)
        

        我在这里简单地应用了。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-18
          • 2013-10-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-13
          相关资源
          最近更新 更多