第 二 章 C++简单程序设计
2-10 执行完下列语句后,a、b、c三个变量的值为多少?
a = 30;
b = a++;
c = ++a;
解:
a:32 ; b:30 ; c:32;
2-13 写一条for语句,计数条件为n从100到200,步长为2;然后用while和do…while语句完成同样的循环。
解:
for循环:
for (int n = 100; n <= 200; n += 2);
while循环:
int x = 100;
while (n <= 200)
n += 2;
do…while循环:
int n = 100;
do
{
n += 2;
} while(n <= 200);
2-17 修改下面这个程序中的错误,改正后它的运行结果是什么?
#include <>
void main()
int i
int j;
i = 10; /* 给i赋值
j = 20; /* 给j赋值 */
cout << "i + j = << i + j; /* 输出结果 */
return 0;
}
解:
改正:
#include <>
int main()
{
int i;
int j;
i = 10; n";
cout << "You are: " << yourAge << " years old\n";
myAge++; .\n";
cout << "I am: " << myAge << " years old.\n";
明解c语言cout << "You are: " << yourAge << " years old\n";
cout << "Another year passes\n";
cout << "I am: " << myAge++ << " years old.\n";
cout << "You are: " << ++yourAge << " years old\n";
cout << "Let's print it again.\n";
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论