C++学习笔记——逻辑运算符

逻辑OR运算符

#include 
int main()
{
    using namespace std;
    cout << "This program may reformat your hard disk\n"
            "and destory all your data.\n"
            " Do you wish to continue?";//c++字符的拼接特性,将一个字符串分布在3行中。
    char ch;
    cin >> ch;
    if (ch == 'y'|| ch == 'Y')
    //||运算符是个顺序点,也就是说先判定左侧的值,再对右侧的值进行判定
    //如果左侧的表达式是true。则c++将不会去判定右侧的表达式。
        cout << "You were warned!\a\a\n";//转义字符\a是响铃,输出\a时会有错误音效。
    else if (ch == 'n'|| ch == 'N')
        cout << "A wish choice ... bye\n";
    else
    cout << "That wasn't a y or n! Apparently you "
            "can't follow\nintructions,So "
            "I'll trash your disk angway.\a\a\a\n";
    return 0;
}
This program may reformat your hard disk
and destory all your data.
 Do you wish to continue?no
A wish choice ... bye
This program may reformat your hard disk
and destory all your data.
 Do you wish to continue?NO!//由于程序只读取一个字符,因此只读取响应的第一个字符。
//这就意味着用户可以输入NO!程序只读取N。但是程序后面再读取输入时,将从O开始。
A wish choice ... bye

逻辑AND运算符 

// and.cpp -- using the logical AND operator
#include 
const int ArSize = 6;
int main()
{
    using namespace std;
    float naaq[ArSize];
    cout << "Enter the NAAQs (New Age Awareness Quotients) "
         << "of\nyour neighbors. 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) //&&运算符也是顺序点,因此首次将判定左侧,并且在右侧被判定之前产生所有的副作用。
    //如果左侧是false,则整个逻辑表达式必定是false,这时,c++不会对右侧进行判定。
    //程序说明:while测试条件是查看数组是否还有空间(i=0),满足条件就将temp的值复制到数组中,
    //并将数组索引+1。也就是说即使当i> temp;            // so get next value
        }
    }
    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";
    }
    // cin.get();
    // cin.get();
    return 0; 
}
Enter the NAAQs (New Age Awareness Quotients) of
your neighbors. Program terminates when you make
6 entries or enter a negative value.
First value: 25
Next value: 46
Next value: 56
Next value: 17
Next value: 17
Next value: 78
Enter your NAAQ: 13.4
6 of your neighbors have greater awareness of
the New Age than you do.
Enter the NAAQs (New Age Awareness Quotients) of
your neighbors. Program terminates when you make
6 entries or enter a negative value.
First value: -8
No data--bye

使用&&来设定取值范围

#include 
    
const char * qualify[4] = 
{
    "10,000-meter race.\n",
    "mud tug-of-war.\n",
    "masters canoe jousting.\n",
    "pie-throwing festival.\n"
};//使用字符串数字来存储4个字符串的地址,使用const限定符可以避免无意中修改这些字符串。
int main()
{
    using namespace std;
    int age;
    cout << "Enter your age in years";
    cin >> age;
    int index;

    if (age > 14 && 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 quality for the " << qualify[index];
    return 0;   
}

Enter your age in years24
You quality for the 10,000-meter race.

取值范围测试

if ( age > 14 && age < 35)//true
if ( 14 < age < 35)//编译器不会捕获这种错误,因为它仍然是有效的c++语法。
//<运算符从左向右结合,if((14 < age) < 35)、17 < age的值要么是true(1),要么是false(0),
//都小于35,整个测试的结果是true。

逻辑NOT运算符:!

#include 
#include 
bool is_int(double);
int main()
{
    using namespace std;
    double num;

    cout << "Yo,dube!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";
    return 0;
}

bool is_int(double x)
{
    if ( x <= INT16_MAX && x >= INT16_MIN)
    //使用了climits文件中定义的两个符号常量INT16_MAX和INT16_MIN来que确定其参数是否达位于合适的范围内
        return true;
    else
        return false;
    
}
Yo,dube!Enter an integer value89494657948689
Out of range -- please try again: 45
You've entered the integer 45
Bye

逻辑运算符的细节

!运算符高于所有的关系运算符和算术运算符:使用时应当注意适时添加括号。

逻辑and运算符的优先级高于or运算符,要想使or运算符高于and,注意使用括号。

一般情况下不使用括号也可以编写负责的复合句,但是最好使用括号将测试进行分组,这样不容易出错,并且逻辑清晰。

你可能感兴趣的:(c++,学习,笔记)