c语言中的逻辑运算符_C / C ++中的逻辑运算符

c语言中的逻辑运算符

逻辑运算符 (Logical Operators)

Logical operators are used to check the combinations of the two conditional expressions.

逻辑运算符用于检查两个条件表达式的组合。

The following are the types of logical operators.

以下是逻辑运算符类型

  1. Logical AND (&&) Operator

    逻辑AND( && )运算符

  2. Logical OR (||) Operator

    逻辑OR( || )运算符

  3. Logical NOT (!) Operator

    逻辑NOT( )运算符

1)逻辑AND(&&)运算符 (1) Logical AND (&&) Operator)

Logical AND operator represented by the symbols "&&", it works with two operands and returns 1 if both operands are true (non-zero); 0, otherwise.

逻辑AND运算符由符号“ && ”表示,它与两个操作数一起使用,如果两个操作数均为true(非零),则返回1;否则,返回1。 0,否则。

Note: Operands can be values, conditions, expressions, etc.

注意 :操作数可以是值,条件,表达式等。

Syntax:

句法:

operand1 && operand2

Truth table:

真相表:

operand1operand2operand1 && operand2
Non-zeroNon-zero1
Non-zero00
0Non-zero0
000
操作数1操作数2操作数1 &&操作数2
非零非零1个
非零00
0非零0
000

C++ program to demonstrate the example of logical AND (&&) operator

C ++程序演示逻辑AND(&&)运算符的示例

#include <iostream>
using namespace std;

int main()
{
    int a = 10;
    int b = 20;

    // printing the values
    cout << "a : " << a << endl;
    cout << "b : " << b << endl;
    cout << endl;

    // Logical AND operations
    cout << "(a && b) : " << (a && b) << endl;
    cout << "(a && 0) : " << (a && 0) << endl;
    cout << "(0 && b) : " << (0 && b) << endl;
    cout << "(0 && 0) : " << (0 && 0) << endl;
    cout << endl;

    cout << "(a >= 10 && b <= 30) : " << (a >= 10 && b <= 30) << endl;
    cout << "(a == 10 && b == 20) : " << (a == 10 && b == 20) << endl;
    cout << "(a >= 10 && b == 30) : " << (a >= 10 && b == 30) << endl;
    cout << "(a < 10 && b < 20)   : " << (a < 10 && b < 20) << endl;

    return 0;
}

Output:

输出:

a : 10
b : 20

(a && b) : 1
(a && 0) : 0
(0 && b) : 0
(0 && 0) : 0

(a >= 10 && b <= 30) : 1
(a == 10 && b == 20) : 1
(a >= 10 && b == 30) : 0
(a < 10 && b < 20)   : 0

2)逻辑或(||)运算符 (2) Logical OR (||) Operator)

Logical OR operator represented with the symbols "||", it works with two operands and returns 1 if one (or both) operands are true (non-zero); 0, otherwise.

用符号“ || ”表示的逻辑“或”运算符 ,可用于两个操作数,如果一个(或两个)操作数为真(非零),则返回1;否则,返回1。 0,否则。

Syntax:

句法:

operand1 || operand2

Truth table:

真相表:

operand1operand2operand1 && operand2
Non-zeroNon-zero1
Non-zero01
0Non-zero1
000
操作数1操作数2操作数1 &&操作数2
非零非零1个
非零01个
0非零1个
000

C++ program to demonstrate the example of logical OR (||) operator

C ++程序演示逻辑OR(||)运算符的示例

#include <iostream>
using namespace std;

int main()
{
    int a = 10;
    int b = 20;

    // printing the values
    cout << "a : " << a << endl;
    cout << "b : " << b << endl;
    cout << endl;

    // Logical OR operations
    cout << "(a || b) : " << (a || b) << endl;
    cout << "(a || 0) : " << (a || 0) << endl;
    cout << "(0 || b) : " << (0 || b) << endl;
    cout << "(0 || 0) : " << (0 || 0) << endl;
    cout << endl;

    cout << "(a >= 10 || b <= 30) : " << (a >= 10 || b <= 30) << endl;
    cout << "(a == 10 || b == 20) : " << (a == 10 || b == 20) << endl;
    cout << "(a >= 10 || b == 30) : " << (a >= 10 || b == 30) << endl;
    cout << "(a < 10 || b < 20)   : " << (a < 10 || b < 20) << endl;

    return 0;
}

Output:

输出:

a : 10
b : 20

(a || b) : 1
(a || 0) : 1
(0 || b) : 1
(0 || 0) : 0

(a >= 10 || b <= 30) : 1
(a == 10 || b == 20) : 1
(a >= 10 || b == 30) : 1
(a < 10 || b < 20)   : 0

3)逻辑非(!)运算符 (3) Logical NOT (!) Operator)

Logical NOT operator represented by the symbols "!", it works with one operand and returns 1 if the operand is zero;0, otherwise.

逻辑NOT运算符以符号“ ”表示,它与一个操作数一起使用,如果操作数为零,则返回1;否则返回0。

Syntax:

句法:

!operand

Truth table:

真相表:

operand!operand
Non-zero0
01
操作数!operand
非零0
01个

C++ program to demonstrate the example of logical NOT (!) operator

C ++程序演示逻辑NOT(!)运算符的示例

#include <iostream>
using namespace std;

int main()
{
    int a = 10;
    int b = 0;

    // printing the values
    cout << "a : " << a << endl;
    cout << "b : " << b << endl;
    cout << endl;

    cout << "!a : " << !a << endl;
    cout << "!b : " << !b << endl;
    cout << endl;

    // Logical NOT operations
    cout << "!(a || b) : " << !(a || b) << endl;
    cout << "!(a || 0) : " << !(a || 0) << endl;
    cout << "!(0 || b) : " << !(0 || b) << endl;
    cout << "!(0 || 0) : " << !(0 || 0) << endl;
    cout << endl;

    cout << "!(a >= 10 || b <= 30) : " << !(a >= 10 || b <= 30) << endl;
    cout << "!(a == 10 || b == 20) : " << !(a == 10 || b == 20) << endl;
    cout << "!(a >= 10 || b == 30) : " << !(a >= 10 || b == 30) << endl;
    cout << "!(a < 10 || b < 20)   : " << !(a < 10 || b < 20) << endl;

    return 0;
}

Output:

输出:

a : 10
b : 0

!a : 0
!b : 1

!(a || b) : 0
!(a || 0) : 0
!(0 || b) : 1
!(0 || 0) : 1

!(a >= 10 || b <= 30) : 0
!(a == 10 || b == 20) : 0
!(a >= 10 || b == 30) : 0
!(a < 10 || b < 20)   : 0

翻译自: https://www.includehelp.com/cpp-tutorial/logical-operators-in-c-cpp.aspx

c语言中的逻辑运算符