一、邏輯OR運算符:||
如果表達式中的任何一個或全部都為true(或非零),則得到的表達式的值為true;否則,表達式的值為false。
||的優先級比關系運算符低。
C++規定,||運算符是個順序點。即,先修改左側的值,再對右側的值進行判定。如果左側的表達式為true,則C++將不會去判定右側的表達式,因為只要一個表達式為true,則整個邏輯表達式為true。
程序清單6.4在一條if語句中使用||運算符來檢查某個字符的大寫或小寫。另外,它還使用了C++運算符的拼接特性(參見第4章)將一個字符串分布在3行中。
#if 1
#include<iostream>
using namespace std;int main()
{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";elsecout << "That wasn't a y or n!Apparently you ""can't follow\ninstructions,so ""I'll trash your disk anyway.\a\a\a\n";system("pause");return 0;
}
#endif