第6章 分支语句和逻辑运算符
- 6.1 if语句
- 6.1.1 if else语句
- 6.1.2 格式化if else语句
- 6.1.3 if else if else结构
- 6.2 逻辑表达式
- 6.2.1 逻辑OR运算符:||
- 6.2.2 逻辑AND运算符:&&
- 6.2.3 用&&来设置取值范围
6.1 if语句
if语句让程序能够决定是否应执行特定的语句。整个if语句被视为一条语句。
// 程序清单6.1 if.cpp--using the if statement
#include <iostream>
using namespace std;
int main(void)
{
char ch;
int spaces = 0;
int total = 0;
cin.get(ch);
while (ch != '.') {
if (ch == ' ') {
++spaces;
}
++total;
cin.get(ch);
}
// 若在输入字符中包含回车,则字符总数中包括回车键生成的换行符
cout << spaces << " spaces, " << total << " characters total in sentence." << endl;
return 0;
}
6.1.1 if else语句
if else语句则让程序决定执行两条语句或语句块中的哪一条。整个if else结构被视为一条语句。
// 程序清单6.2 ifelse.cpp--using the if else statement
#include <iostream>
using namespace std;
int main(void)
{
char ch;
cout << "Type, and I shall repeat." << endl;
cin.get(ch);
while (ch != '.') {
if (ch == '\n') {
cout << ch;
} else {
cout << ++ch;
// cout << ch + 1; 此情况cout将显示int值,不是char字符
}
cin.get(ch);
}
cout << endl << "Please excuse the slight confusion." << endl;
return 0;
}
6.1.2 格式化if else语句
if else中的两种操作都必须是一条语句。如果需要多条语句,需要用大括号将它们括起来,组成一个块语句。
6.1.3 if else if else结构
整个构造仍被视为一条语句。
// 程序清单6.3 ifelseif.cpp--using if else if else
#include <iostream>
using namespace std;
int main(void)
{
const int Fave = 27;
int n;
cout << "Enter a number in the range 1-100 to find my favorite number: ";
do {
cin >> n;
if (n < Fave) {
cout << "Too low -- guess again: ";
} else if (n > Fave) {
cout << "Too high -- guess again: ";
} else {
cout << Fave << " is right!" << endl;
}
} while (n != Fave);
return 0;
}
6.2 逻辑表达式
3种逻辑运算符:逻辑OR(||)、逻辑AND(&&)、逻辑NOT(!)
6.2.1 逻辑OR运算符:||
C++可以采用逻辑OR运算符(||),将两个表达式组合在一起。如果原来表达式中的任何一个或全部都为true,则得到的表达式的值为true;否则,表达式的值为false。
C++规定,||运算符是个顺序点。也就是说,先修改左侧的值,再对右侧的值进行判定(C++11的说法是,运算符左边的子表达式先于右边的子表达式)
i++ < 6 || i == j
// 假设i原来的值为10,则在对i和j进行比较时,i的值将为11
// 如果左侧的表达式为true,则C++将不会去判定右侧的表达式,因为只要一个表达式为true,则整个逻辑表达式为true
// 程序清单6.4 or.cpp--using the logical OR operator
#include <iostream>
using namespace std;
int main(void)
{
cout << "This program may reformat your hard disk and destroy all your data." << endl;
cout << "Do you wish to continue?<y/n> ";
char ch;
cin >> ch;
if (ch == 'y' || ch == 'Y') {
cout << "Your were warned!\a\a" << endl;
} else if (ch == 'n' || ch == 'N') {
cout << "A wise choise ... bye" << endl;
} else {
cout << "That wasn't a y or n! Apparently you can't follow\ninstructions, so I'll trash your disk anyway.\a\a\a" << endl;
}
return 0;
}
6.2.2 逻辑AND运算符:&&
逻辑AND运算符(&&),也是将两个表达式组合成一个表达式。仅当原来的两个表达式都为true时,得到的表达式的值才为true。
&&运算符也是顺序点,因此将首先判定左侧,并且在右侧被判定之前产生所有的副作用。如果左侧为false,则整个逻辑表达式必定为false,在这种情况下,C++将不会再对右侧进行判定。
// 程序清单6.5 and.cpp--using the logical AND operator
#include <iostream>
using namespace std;
int main(void)
{
const int Arsize = 6;
float naaq[Arsize];
cout << "Enter the NAAQs (New Age Awareness Quotients) of\n"
<< "your neighbors. Pragram terminates when you make\n"
<< Arsize << " entries or enter a negative value." << endl;
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" << endl;
} 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 << " of your neighbors have greater awareness of the New Age than you do." << endl;
}
return 0;
}
6.2.3 用&&来设置取值范围
// 程序清单6.6 more_and.cpp--using the logical AND operator
#include <iostream>
using namespace std;
int main(void)
{
const char *qualify[4] = {
"10,000-meter race.\n",
"mud tug-of-war.\n",
"masters canoe jousting.\n",
"pie-throwing festival.\n"
};
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 << "Your qualify for the " << qualify[index];
return 0;
}
在使用取值范围测试时,应确保取值范围之间既没有缝隙,又没有重叠。
取值范围测试的每一部分都使用AND运算符将两个完整的关系表达式组合起来:
if (age > 17 && age < 35) // OK
不要使用数学符号将其表示为:
if (17 < age < 35)
编译器不会捕获这种错误,因为它仍然是有效的C++语法。
<运算符从左向右结合,因此上述表达式的含义如下:
if ((17 < age) < 35)
但17 < age的值要么为true(1),要么为false(0)。不管是哪种情况,表达式17 < age的值都小于35,因此整个测试的结果总是true!