贵哥卤肉卷的菜单:c++语句
来源:百度文库 编辑:九乡新闻网 时间:2024/04/28 02:03:38
interest = balance * creditRate;
balance += interest;
}
else {
interest = balance * debitRate;
balance += interest;
}
结合现实我们知道,一个条件的成立与否往往会需要执行两种不同的动作,比如:公交车有否上下客,决定汽车停或继续行驶。为此,C++设立了if 语句的变种,即当给定的条件满足时,执行一个语句;当条件不满足时,执行另一个语句。这也被称为if-else语句。
例如:
if (有上下客)
汽车停;
else
继续行驶;
同样,语句1或语句2也都可以是复合语句,如:
if (点不被多边形遮挡)
{
把点从原始点表中取出;
把点加入到显示点表中;
}
else
{
把点从原始点表中取出;
把点加入到非显示点表中;
}
例4-2 if (balance > 0)
interest = balance * creditRate;
else
interest = balance * debitRate;
balance += interest;
我们还可以使用条件表达式将上述语句进一步简化为:
interest = balance * (balance > 0 ? creditRate : debitRate);
balance += interest;
或:
balance += balance * (balance > 0 ? creditRate : debitRate); 同样考虑到两个复合语句中的共同部分,我们可以作如下简化:
把点从原始点表中取出;
if (点不被多边形遮挡)
{
把点加入到显示点表中;
}
else
{
把点加入到非显示点表中;
}
也可以使用上一章的条件运算符将上述语句进一步简化为:
把点从原始点表中取出;
点不被多边形遮挡? 把点加入到显示点表中: 把点加入到显示点表中;
例4-3 if (callHour > 6) {
if (callDuration <= 5)
charge = callDuration * tarrif1;
else
charge = 5 * tarrif1 + (callDuration - 5) * tarrif2;
}
else
charge = flatFee; 现实生活中的各种条件是很复杂的,在一定的条件下,又需要满足其它的条件才能确定相应的动作。为此,C++ 提供了if语句的嵌套功能,也即一个if语句能够出现在另一个if语句或if-else语句里。例如: 例4-4 if (callHour > 6) {
if (callDuration <= 5)
charge = callDuration * tarrif1;
else
charge = 5 * tarrif1 + (callDuration - 5) * tarrif2;
}
else
charge = flatFee; 另一例: 例4-5 if ( cos( value ) < 0 ) // value 的cos函数小于零
{
if ( value > 0 ) // 若value 大于零
value = value - PI;
else
value = value + PI;
}
例4-6 if (ch >= '0' && ch <= '9')
kind = digit;
else {
if (ch >= 'A' && ch <= 'Z')
kind = upperLetter;
else {
if (ch >= 'a' && ch <= 'z')
kind = lowerLetter;
else
kind = special;
}
} 为了改进这条语句的可读性,我们可以将语句写成下面的形式: 例4-7 if (ch >= '0' && ch <= '9')
kind = digit;
else if (cha >= 'A' && ch <= 'Z')
kind = capitalLetter;
else if (ch >= 'a' && ch <= 'z')
kind = smallLetter;
else
kind = special;
例如,下面的if语句: 例4-8 if ( x < 0 ) y = -1;
else if ( x == 0 ) y = 0;
else y = 1;
我们可以把它修改为:
if ( x >= 0)
if ( x > 0 ) y = 1;
else y = 0;
else y = -1;
例4-9 switch (operator) {
case '+': result = operand1 + operand2;
break;
case '-': result = operand1 - operand2;
break;
case '*': result = operand1 * operand2;
break;
case '/': result = operand1 / operand2;
break;
default: cout << "unknown operator: " << ch << '\n';
break;
} 从上例我们可以看到:switch语句中的每一个case的结尾通常有一个break语句,它停止switch语句的继续执行,而转向该switch语句的下一个语句。但是,我们有时也有意不加break语句。例如,我们修改上面的例子,以允许x作为乘法运算符,则可将上面的程序修改为: 例4-10 switch (operator) {
case '+': result = operand1 + operand2;
break;
case '-': result = operand1 - operand2;
break;
case 'x':
case '*': result = operand1 * operand2;
break;
case '/': result = operand1 / operand2;
break;
default: cout << "unknown operator: " << ch << '\n';
break;
} 因为case 'x'没有break语句,所以case 'x'与case '*'执行相同的语句系列,也就是说,把'x'扩展成为乘法运算符了。
很显然,switch语句可以写成if-else语句的形式。例如,上面的语句,我们可以写为: 例4-11 if (operator == '+')
result = operand1 + operand2;
else if (operator == '-')
result = operand1 - operand2;
else if (operator == 'x' || operator == '*')
result = operand1 * operand2;
else if (operator == '/')
result = operand1 / operand2;
else
cout << "unknown operator: " << ch << '\n';
但是,使用switch语句比用if-else语句简洁得多,可读性也好得多。遇到多分支选择的情形,我们应当尽量选用switch语句,避免采用嵌套较深的if-else语句。
下面我们通过一个具体例子来进一步加深对if-else控制结构的认识,程序的功能是:要求用户输入一个整数,整数范围为1~5之间,然后,计算机根据整数的值,发出鸣叫声。例如,输入数字1,则鸣叫一声,输入数字2,则鸣叫两声,…,如果是1~5之外的数,则没有叫声。程序中定义了一个宏BEEP,当程序中遇到BEEP,便用cout << "\a \n"替换,有关宏的内容在第9章详细介绍。\a是一个转义字符,它使计算机发出鸣叫声。 例4-12 #include
#define BEEP cout << "\a \n"
void main()
{
int num;
cout << "Please enter a number: ";
cin >> num;
// Use multiple if statements to beep.
if (num == 1){
BEEP;
}
else if (num == 2){
BEEP;BEEP;
}
else if (num == 3){
BEEP; BEEP; BEEP;
}
else if (num == 4){
BEEP; BEEP; BEEP; BEEP;
}
else if (num == 5){
BEEP; BEEP; BEEP; BEEP; BEEP;
}
}
例4-13 #include
#define BEEP cout << "\a \n"
void main()
{
int num;
cout << "Please enter a number: ";
cin >> num;
switch (num)
{
case 1: BEEP;
break;
case 2: BEEP; BEEP;
break;
case 3: BEEP; BEEP; BEEP;
break;
case 4: BEEP; BEEP; BEEP; BEEP;
break;
case 5: BEEP; BEEP; BEEP; BEEP; BEEP;
}
}
while语句
while语句的一般形式如下:
while (表达式)
语句;
while语句的执行过程如下:首先计算"表达式"(称之为循环条件)的值,如果其结果值非0,则执行"语句"(称之为循环体)。这个过程重复进行,称之为循环,直至"表达式"的值为0时,才结束循环。
2 do-while语句
do-while语句类似于while语句,但是它先执行循环体,然后检查循环条件。do-while语句的一般形式为:
do
语句;
while (表达式);
如果"表达式"的值非0,循环继续进行,否则,循环终止。
与while语句相比,我们使用do-while语句要少一些。但是,对于有些情况,循环体至少要执行一次,do-while语句就很有用。例如,假定我们要重复读一个值,并打印它的平方,终止的条件是该值为0。我们可以用do-while语句表示为:
do {
cin >> n;
cout << n * n << '\n';
}
while (n != 0);
例4-15 // 无break语句
int sum = 0, number;
cin >> number;
while (number != 0) {
sum += number;
cin >> number;
}
// 有break语句
int sum = 0, number;
while (1) {
cin >> number;
if (number == 0)
break;
sum += number;
} 这两段程序的产生的效果是一样的。需要注意的是:break语句只是跳出当前的循环语句,对于嵌套的循环语句,break语句的功能是从内层循环跳到外层循环。例如4-16: 例4-16
int i = 0, j, sum = 0;
while (i < 5) {
for ( j = 0; j < 5; j++) {
sum + = i + j;
if ( j == i) break;
}
i++;
}
本例中break语句执行后,程序立即终止for循环语句,并转向for循环语句的下一个语句,即while循环体中的i++语句,继续执行while循环语句。
终止当前的循环语句意思是在多重循环中,break只能退出它所在的那重循环;只能退出内重循环到外重循环中,要退出外重循环还要在外重循环中使用break。
cin >> num;
if (num < 0) continue;
// 处理num...
} while (num != 0);
它等价于:
do {
cin >> num;
if (num <= 0) {
// 处理num...
}
} while (num != 0); 如果我们循环终止的条件改为读n个整数,而不是等到某个整数为0,上述代码可以修改为: 程序段4-2 for (i = 0; i < n; ++i) {
cin >> num;
if (num < 0) continue; // 跳转到: ++i
// 处理num...
} 程序:
根据上面的分析,我们可以编写出完整的程序。
简单的计算器 #include
int main()
{
double displayed_value;
double new_entry;
char command_character;
displayed_value = 0.0;
cout << "计算器演示程序" << endl
<< "----" << endl;
cout << "在'>' 提示后输入一个命令字符"
<< endl;
cout << "Value : " << displayed_value << endl;
cout << "command>";
cin >> command_character;
while (command_character != 'Q') {
switch(command_character) {
case 'C':
displayed_value = 0.0;
break;
case '+':
cout << "number>";
cin >> new_entry;
displayed_value += new_entry;
break;
case '-':
cout << "number>";
cin >> new_entry;
displayed_value -= new_entry;
break;
case '*':
cout << "number>";
cin >> new_entry;
displayed_value *= new_entry;
break;
case '/':
cout << "number>";
cin >> new_entry;
displayed_value /= new_entry;
break;
default:
cout << "不可理解的输入!";
cin.ignore(100,'\n');
} //End of switch
cout << "Value : " << displayed_value << endl;
cout << "command>";
cin >> command_character;
} //End of while
return 0;
}
计算器演示程序
----
在'>' 提示后输入一个命令字符
Value : 0
command>+↙
number>1.23↙
Value : 1.23
command>*↙
number>6.4↙
Value : 7.872
command>C↙
Value : 0
command> Q↙
键入'Q'后,程序运行终止。
例5-7
#include
int area(int length, int width); // area函数说明
void main() //主函数,程序从该函数开始执行
{
int this_length, this_width;
cout << "Enter the length: "; //调用cout和cin库函数
cin >> this_length;
cout << "Enter the width: ";
cin >> this_width;
cout << "\n";
cout << "The area of a " << this_length << "x" << this_width;
cout << " rectangle is " << area(this_length, this_width); //调用area函数
}
int area(int length, int width) // area函数定义
{
int number;
number = length * width;
return number;
}
例5-11
int Normalize (void)
{ //... }
int Solve (void)
{ //...
Normalize();
//...
}
int main (void)
{
//...
Solve();
//...
}
应该注意到:调用一个函数,涉及到为该函数在栈中分配内存和释放内存的开销。对于大多数的函数而言,同函数的实际计算量相比可以忽略不计。 例5-12
#include
void reverse(int n)
{
int num;
if(n<=1)
{
cin>>num;
cout<<"Reverse numbers"<
else
{
cin>>num;
reverse(n-1);
cout<
}
void main()
{
int i;
i=5;
cout<<"Enter number"<
}
for循环通常用于有确定次数的循环。例如,下面的for循环语句用于计算整型数1~n的和:
sum = 0;
for (i = 1; i <= n; ++i)
sum += i;
本例中,i通常称为循环变量,注意它并不在循环体内。C++也允许for语句的"表达式1"是一个变量定义。例如,上面的循环语句我们可以表示为:
for (int i = 1; i <= n; ++i)
sum += i;
for语句中三个表达式中任一个均可以省略。例如,省略第一和第三个表达式:
for (; i != 0;) // 等价于: while (i != 0)
语句;
如果把三个表达式都省略,则循环条件为1,循环无限次地进行,即死循环。
for (;;) //等价于: while (1)
语句;
for循环可以有多个循环变量,此时,循环变量的表达式之间用逗号隔开:
for (i = 0, j = 0; i + j < n; ++i, ++j)
语句;
循环语句能够在另一个循环语句的循环体内,即循环能够被嵌套。例如:
for (int i = 1; i <= 3; ++i)
for (int j = 1; j <= 3; ++j)
cout << '(' << i << ',' << j << ")\n";
本段程序运行结果如下:
(1,1) (1,2) (1,3)
(2,1) (2,2) (2,3)
(3,1) (3,2) (3,3)