【C++】学习笔记二十二——逻辑表达式

逻辑表达式


逻辑或(OR)运算符:||

如果表达式中的任何一个或全部都为true(或非零),则表达式的值为true;否则表达式的值为false。

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

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

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

i++<6||i==j

假设i原来的值为10,则在对i和j进行比较是,i的值将为11。
另外,如果左侧的表达式为true,则C++不会判定右侧的表达式,因为只要有一个表达式值为true,则整个逻辑表达式为true。

程序6.4

#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)运算符:&&

仅当原来的两个表达式都为true时,得到的表达式的值才为true。

程序6.5

#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;
}

一次在输入6个值后结束:
这里写图片描述
一次在输入负值后结束:
这里写图片描述


用&&来设置取值范围

#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)运算符:!

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

程序6.7

#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


其他表示方式

在程序中包含头文件iso646.h,就可用下表的表示方式:

运算符另一种表示方式
&&and
||or
!not
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值