C++PrimerPlus 第六章 分支语句和逻辑运算符 - 6.1 if语句
6.1 if语句
当C++程序必须决定是否执行某个操作时,通常使用if语句来实现选择。if有两种格式:if和if else。首先看一看简单的if,它模仿英语,如“if you have a Captain Cookie card, you get a free cookie(如果您有一张Captain Cookie卡,就可获得免费的小甜饼)”。如果测试条件为true,则if语句将引导程序执行语句或语句块;如果条件是false,程序将跳过这条语句或语句块。因此,if语句让程序能够决定是否应执行特定的语句。
if语句的语法与while相似:
if (test-condition)
statement
如果test-condition(测试条件)为true,则程序将执行statement(语句),后者既可以是一条语句,也可以是语句块。如果测试条件为false,则程序将跳过语句(参见下图)。和循环测试条件一样,if测试条件也将被强制转换为bool值,因此0将被转换为false,非零为true。整个if语句被视为一条语句。
通常情况下,测试条件都是关系表达式,如那些用来控制循环的表达式。例如,假设读者希望程序计算输入中的空格数和字符总数,则可以在while循环中使用cin.get(char)来读取字符,然后使用if语句识别空格字符并计算其总数。程序清单6.1完成了这项工作,它使用句点(.)来确定句子的结尾。
程序清单6.1 if.cpp
//if.cpp -- using the if statement
#include<iostream>
int main()
{
using std::cin; //using declarations
using std::cout;
char ch;
int spaces = 0;
int total = 0;
cin.get(ch);
while (ch != '.') //quit at end of sentence
{
if (ch == ' ') //check if ch is a space
++spaces;
++total; //done every time
cin.get(ch);
}
cout << spaces << " spaces, " << total;
cout << " characters total in sentence\n";
return 0;
}
下面是该程序的输出:
The balloonist was an airhead
with lofty goals.
6 spaces, 46 characters total in sentence
正如程序中的注释指出的,仅当ch为空格时,语句++spaces;才被执行。因为语句++tota;位于if语句的外面,因此在每轮循环中都将被执行。注意,字符总数中包括按回车键生成的换行符。
6.1.1 if else语句
if语句让程序决定是否执行特定的语句或语句块,而if else语句则让程序决定执行两条语句或语句中的哪一条,这种语句对于选择其中一种操作很有用。C++的if else语句模仿了简单的英语,如“If you have a Captain Cookie card, you get a Cookie Plus Plus, else you just get a Cookie d’Ordinarie(如果您拥有Captain Cookie卡,将可获得Cookie Plus Plus,否则只能获得Cookie d’Ordinaire)”。if else语句的通用格式如下:
if (test-condition)
statement1
else
statement2
如果测试条件为true或非零,则程序将执行statement,跳过statement2;如果测试条件为false或0,则程序将跳过statement1,执行statement2。因此,如果answer是1492,则下面的代码片段将打印第一条信息,否则打印第二条信息:
if (answer == 1492)
cout << “That’s right!\n”;
else
cout << “You’d better review Chapter 1 again.\n”;
每条语句都既可以是一条语句,也可以是用大括号括起的语句块(参见下图)。从语法上看,整个if else结构被视为一条语句。
假如,假设要通过对字母进行加密编码来修改输入的文本(换行符不变)。这样,每个输入行都被转换为一行输出,且长度不变。这意味着程序对换行符采用一种操作,而对其他字符采用另一种操作。正如程序清单6.2所表明的,if else使得这项工作非常简单。该程序清单还演示了限定符std::,这是编译指令using的替代品之一。
程序清单6.2 ifelse.cpp
//ifelse.cpp -- using the if else statement
#include<iostream>
int main()
{
char ch;
std::cout << "Type, and I shall repeat.\n";
std::cin.get(ch);
while (ch != '.')
{
if (ch == '\n')
std::cout << ch; //done if newline
else
std::cout << ++ch; //done otherwise
std::cin.get(ch);
}
//try ch + 1 instead of ++ch for interesting effect
std::cout << "\nPlease excuse the slight confusion.\n";
//std::cin.get();
//std::cin.get();
return 0;
}
下面是该程序的运行情况:
Type, and I shall repeat.
An ineffable joy suffused me as I beheld
Bo!jofggbcmf!kpz!tvggvtfe!nf!bt!J!cfifme
the wonders of modern computing.
uif!xpoefst!pg!npefso!dpnqvujoh
Please excuse the slight confusion.
注意,程序清单6.2中的注释之一指出,将++ch改为ch+1将产生一种有趣的效果。能推断它是什么吗?如果不能,就试验一下,然后看看是否可以解释发生的情况(提示:想一想cout是如何处理不同的类型的)。
6.1.2 格式化if else语句
if else中的两种操作都必须是一条语句。如果需要多条语句,需要用大括号将它们括起来,组成一个语句块。和有些语言(如BASIC和FORTRAN)不同的是,由于C++不会自动将if和else之间的所有代码视为一个代码块,因此必须使用大括号将这些语句组合成一个语句块。例如,下面的代码将出现编译器错误:
if (ch == ‘Z’)
zorro++; //if ends here
cout << “Another Zorro candidate\n”;
else //wrong
dull++;
cout << “Not a Zorro candidate\n”;
编译器把它看作是一条以zorro++;语句结尾的简单if语句,接下来是一条cout语句。到目前为止,一切正常。但之后编译器发现一个独立的else,这被视为语法错误。
请添加大括号,将语句组合成一个语句块:
if (ch == ‘Z’)
{ //if true block
zorro++;
cout << “Another Zorro candidate\n”;
}
else
{ //if false block
dull++;
cout << “Not a Zorro candidate\n”;
}
由于C++是自由格式语言,因此只要使用大括号将语句括起,对大括号的位置没有任何限制。上述代码演示了一种流行的格式,下面是另一种流行的格式:
if (ch == ‘Z’){
zorro++;
cout << “Another Zorro candidate\n”;
}
else{
dull++;
cout << “Not a Zorro candidate\n”;
}
第一种格式强调的是语句的块结构,第二种格式则将语句块与关键字if和else更紧密地结合在一起。这两种风格清晰、一致,应该能够满足要求;然而,可能会有老师或雇主在这个问题上的观点强硬而固执。
6.1.3 if else if else结构
与实际生活中发生的情况类似,计算机程序也可能提供两个以上的选择。可以将C++的if else语句进行扩展来满足这种需求。正如读者知道的,else之后应是一条语句,也可以是语句块。由于if else语句本身是一条语句,所以可以放在else的后面:
if (ch == ‘A’)
a_grade++; //alternative # 1
else
if (ch == ‘B’) //alternative # 2
b_grade++; //subalternative # 2a
else
soso++; //subalternative # 2b
如果ch不是’A’,则程序将执行else。执行到那里,另一个if else又提供了两种选择。C++的自由格式允许将这些元素排列成便于阅读的格式:
if (ch == ‘A’)
a_grade++; //alternative # 1
else if (ch == ‘B’)
b_grade++; //alternative # 2
else
soso++; //alternative # 3
这看上去像是一个新的控制结构——if else if else结构。但实际上,它只是一个if else被包含在另一个if else中。修订后的格式更为清晰,使程序员通过浏览代码便能确定不同的选择。整个构造仍被视为一条语句。
程序清单6.3使用这种格式创建了一个小型测试程序。
程序清单6.3 ifelseif.cpp
//ifelseif.cpp -- using if else if else
#include<iostream>
const int Fave = 27;
int main()
{
using namespace std;
int n;
cout << "Enter a number in the range 1-100 to find ";
cout << "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!\n";
} while (n != Fave);
return 0;
}
下面是该程序的输出:
Enter a number in the range 1-100 to find my favorite number: 50
Too high -- guess again:25
Too low -- guess again: 37
Too high -- guess again:31
Too high -- guess again:28
Too high -- guess again:27
27 is right!
条件运算符和错误防范
许多程序员将更直观的表达式variable==value反转为value==variable,以此来捕获将相等运算符误写为赋值运算符的错误。例如,下述条件有效,可以正常工作:
if (3 == myNumber)
但如果错误地使用下面的条件,编译器将生成错误消息,因为它以为程序员试图将一个值赋给一个字面值(3总是等于3,而不能将另一个值赋给它):
if (3 = myNumber)
假设犯了类似的错误,但使用的是前一种表示方法:
if (myNumber = 3)
编译器将只是把3赋给myNumber,而if中的语句块将包含非常常见的、而又非常难以发现的错误(然而,很多编译器会发出警告,因此注意警告是明智的)。一般来说,编写让编译器能够发现错误的代码,比找出导致难以理解的错误的原因要容易得多。