逻辑运算 与&& 或|| 非!

一 逻辑与 &&

image

都为真,逻辑与才是真
只要有一个是假, 逻辑与就是假

特别注意:
条件1 && 条件2
当条件1为真时,才去判断条件2
当条件1为假时,就不再判断条件2

#include <iostream>
#include <Windows.h>

using namespace std;

int main()
{
    int money = 0;
    int days = 0;

    cout << "你有多少钱?";
    cin >> money;
    cout << "你有多少时间?";
    cin >> days;

    if (money >= 100000 && days > 10)
    {
        cout << "我要去旅游" << endl;
    }
    else
    {
        cout << "我还是宅在家里吧" << endl;
    }

    system("pause");
    return 0;
}

image

二 逻辑或 ||

image
只要有一个是真, 结果就是真
都为假时,结果才是假

#include <iostream>
#include <Windows.h>

using namespace std;

int main()
{
    int money = 0;
    string str;

    cout << "你有多少钱:";
    cin >> money;
    cout << "你爱我吗?";
    cin >> str;

    if (money >= 500000 || str == "爱")
    {
        cout << "我们结婚吧" << endl;
    }
    else
    {
        cout << "你是一个好人" << endl;
    }

    system("pause");
    return 0;
}

image

三 逻辑非 !

image

特别注意:
逻辑非,只对一个条件进行运算!
是一种“单目运算符”

#include <iostream>
#include <Windows.h>

using namespace std;

int main()
{
    int salary = 0;
    
    cout << "你月薪多少?";
    cin >> salary;

    if (!(salary >= 30000))
    {
        cout << "我是菜鸟,月薪不到3万,我要努力修炼" << endl;
    }
    else
    {
        cout << "我是老鸟,我要接外包" << endl;
    }

    system("pause");
    return 0;
}

image

posted @ 2022-04-06 23:13  荒年、  阅读(61)  评论(0编辑  收藏  举报