運算符是邏輯或,寫成||;邏輯與,寫&&和邏輯非,寫成!。
OR可以指示兩種條件之一或兩者滿足要求。
邏輯或運算符,寫入||,將兩個表達式組合成一個。
如果一個或兩個原件表達式為true或非零,則生成的表達式的值為true。
否則,表達式的值為false。
這里有些例子:
5 == 5 || 5 == 9 // true because first expression is true 5 > 3 || 5 > 10 // true because first expression is true 5 > 8 || 5 < 10 // true because second expression is true 5 < 8 || 5 > 2 // true because both expressions are true 5 > 8 || 5 < 2 // false because both expressions are false
因為||具有比關(guān)系運算符低的優(yōu)先級,您不需要在這些表達式中使用括號。
下表總結(jié)了||運算符的工作原理。
expr1的值|| expr2
expr1 == true expr1 == false expr2 == true true true expr2 == false true false
C ++||運算符支持快捷方式評估。
例如,考慮以下表達式:
i++ < 6 || i == j
假設(shè) i 最初的值為10。
如果表達式i ++<6是真的,C++將不會麻煩評估表達式 i== j,因為它只需要一個真實的表達式來使整個邏輯表達式為真。
以下代碼在if語句中使用||運算符來檢查字符的大寫和小寫版本。
#include <iostream>
using namespace std;
int main()
{
cout << "Do you wish to continue? <y/n> ";
char ch;
cin >> ch;
if (ch == "y" || ch == "Y") // y or Y
cout << "You were warned!\a\a\n";
else if (ch == "n" || ch == "N") // n or N
cout << "A wise choice ... bye\n";
else
cout << "else";
return 0;
}
上面的代碼生成以下結(jié)果。
邏輯與運算符&&將兩個表達式組合成一個。
如果兩個原始表達式都為true,則生成的表達式的值為true。
這里有些例子:
5 == 5 && 4 == 4 // true because both expressions are true 5 > 3 && 5 > 10 // false because second expression is false
下表總結(jié)了&&運算符的工作原理。
expr1 && expr2的值
expr1 == true expr1 == false expr2 == true true false expr2 == false false false
以下代碼顯示如何使用&&
#include <iostream>
const int SIZE = 6;
using namespace std;
int main()
{
float naaq[SIZE];
int i = 0;
float temp;
cout << "First value: ";
cin >> temp;
while (i < SIZE && temp >= 0) // 2 quitting criteria
{
naaq[i] = temp;
++i;
if (i < SIZE) // room left in the array,
{
cout << "Next value: ";
cin >> temp; // so get next value
}
}
return 0;
}
上面的代碼生成以下結(jié)果。
!運算符否定后面的表達式的真值。
如果expression為true,則!expressionis為false,反之亦然。
以下代碼顯示如何使用not運算符。
#include <iostream>
#include <climits>
using namespace std;
bool is_int(double);
int main()
{
double num;
cout << "Enter an integer value: ";
cin >> num;
while (!is_int(num)) // continue while num is not int-able
{
cout << "Out of range -- please try again: ";
cin >> num;
}
int val = int (num); // type cast
cout << "You"ve entered the integer " << val << "\nBye\n";
return 0;
}
bool is_int(double x)
{
if (x <= INT_MAX && x >= INT_MIN) // use climits values
return true;
else
return false;
}
上面的代碼生成以下結(jié)果。
下面的代碼演示了cctype系列的一些函數(shù)。
#include <iostream>
#include <cctype> // prototypes for character functions
int main(){
using namespace std;
cout << "Enter text for analysis, and type @ to terminate input.\n";
char ch;
int whitespace = 0;
int digits = 0;
int chars = 0;
int punct = 0;
int others = 0;
cin.get(ch); // get first character
while (ch != "@") // test for sentinel
{
if(isalpha(ch)) // is it an alphabetic character?
chars++;
else if(isspace(ch)) // is it a whitespace character?
whitespace++;
else if(isdigit(ch)) // is it a digit?
digits++;
else if(ispunct(ch)) // is it punctuation?
punct++;
else
others++;
cin.get(ch); // get next character
}
cout << chars << " letters, "
<< whitespace << " whitespace, "
<< digits << " digits, "
<< punct << " punctuations, "
<< others << " others.\n";
return 0;
}
上面的代碼生成以下結(jié)果。
下表總結(jié)了cctype包中提供的函數(shù)。
函數(shù)名稱 | 返回值 |
---|---|
isalnum() | 如果參數(shù)是字母數(shù)字(即字母或數(shù)字),則返回true。 |
isalpha() | 如果參數(shù)是字母,則返回true。 |
isblank() | 如果參數(shù)是空格或水平制表符,則返回true。 |
iscntrl() | 如果參數(shù)是控制字符,則返回true。 |
isdigit() | 如果參數(shù)是十進制數(shù)字(0-9),則返回true。 |
isgraph() | 如果參數(shù)是除空格之外的任何打印字符,則返回true。 |
islower() | 如果參數(shù)是小寫字母,則返回true。 |
isprint() | 如果參數(shù)是任何打印字符,則返回true,包括空格。 |
ispunct() | 如果參數(shù)是標(biāo)點符號,則返回true。 |
isspace() | 如果參數(shù)是標(biāo)準(zhǔn)空格字符(即,空格,換頁,換行,回車,水平制表符,垂直制表符),則返回true。 |
isupper() | 如果參數(shù)是大寫字母,則返回true。 |
isxdigit() | 如果參數(shù)是十六進制數(shù)字字符(即,0-9,a-f或A-F),則返回true。 |
tolower() | 如果參數(shù)是大寫字符,tolower()返回該字符的小寫版本;否則,它返回參數(shù)不變。 |
toupper() | 如果參數(shù)是小寫字符,則toupper()返回該字符的大寫版本;否則,它返回參數(shù)不變。 |
更多建議: