# 【C++ Primer plus 第6版】学习笔记二十二——逻辑表达式

2017-01-04 08:19:51来源:CSDN作者:budf01人点击

# 逻辑表达式

## 逻辑或(OR)运算符：||

``5==5||5==9;            //true5>3||5>10;             //true5>8||5<10;             //true5<8||5>2;              //true5>8||5<2;              //false``

||的优先级比运算关系符低，因此不需要括号。

||运算符是个顺序点（sequence point）。也就是说，运算符左边的子表达式先于右边的子表达式。

``i++<6||i==j``

``#include<iostream>int main(){    using namespace std;    cout << "This program may reformat your hard disk/n"        "and destroy all your data./n"        "Do you wish to continue? <y/n> ";    char ch;    cin >> ch;    if (ch == 'y' || ch == 'Y')        cout << "You were warned!/a/a/n";    else if (ch == 'n' || ch == 'N')        cout << "A wise choice ... bye/n";    else        cout << "That wasn't a y or n! Apparently you "        "can't follow/n instructions, so "        "I'll trash your disk anyway./a/a/a/n";    system("pause");    return 0;}``

## 逻辑与(AND)运算符：&&

``#include<iostream>const int ArSize = 6;int main(){    using namespace std;    float naaq[ArSize];    cout << "Enter the NAAQs (New Age Awareness Quotients) "        << "of /nyour nerghbors. Program terminates "        << "when you make/n" << ArSize << " entries "        << "or enter a negative value./n";    int i = 0;    float temp;    cout << "First value: ";    cin >> temp;    while (i < ArSize && temp >= 0)    {        naaq[i] = temp;        ++i;        if (i < ArSize)        {            cout << "Next value: ";            cin >> temp;        }    }    if (i == 0)        cout << "No data--bye/n";    else    {        cout << "Enter your NAAQ: ";        float you;        cin >> you;        int count = 0;        for (int j = 0; j < i; j++)            if (naaq[j] > you)                ++count;        cout << count;        cout << " of your neighbors have greater awareness of /n"            << "the New Age than you do./n";    }    system("pause");    return 0;}``

## 用&&来设置取值范围

``#include<iostream>const char * qualify[4] ={    "10,000-meter race./n",    "mud tug-of-war./n",    "masters canoe jousting./n",    "pie-throwing festival./n"};int main(){    using namespace std;    int age;    cout << "Enter your age in years: ";    cin >> age;    int index;    if (age > 17 && age < 35)        index = 0;    else if (age >= 35 && age < 50)        index = 1;    else if (age >= 50 && age < 65)        index = 2;    else        index = 3;    cout << "You quailify for the " << qualify[index];    system("pause");    return 0;}``

## 逻辑非(NOT)运算符：！

!运算符将它后面的表达式的真值取反。

``#include<iostream>#include<climits>bool is_int(double);int main(){    using namespace std;    double num;    cout << "Yo, dude! Enter an integer value: ";    cin >> num;    while (!is_int(num))    {        cout << "Out of range -- please try again: ";        cin >> num;    }    int val = int(num);    cout << "You've entered the integer " << val << "/nBye/n";    system("pause");    return 0;}bool is_int(double x){    if (x <= INT_MAX&&x >= INT_MIN)        return true;    else        return false;}``

## 逻辑运算符优先级

OR和AND运算符优先级低于关系运算符，因此`x > 5 && x < 10`相当于`(x > 5) && (x < 10)`

!运算符优先级高于所有的关系运算符和算术运算符，因此对表达式求反，必须用括号括起。

AND运算符的优先级高于OR运算符，因此`age >30 && age < 45 || weight > 300`相当于`(age > 30 && age <45) || weight > 300`

&& and
|| or
! not