int i=9;
int res=i++ +1;
cout<<res<<endl;
结果:res=10;
(当++在i的后面,先使用i的值,再自增)
(当++在i的前面,先自增,再使用i的值)
int i=9;
int res=i++ +1;
cout<<res<<endl;
结果:res=10;
(当++在i的后面,先使用i的值,再自增)
(当++在i的前面,先自增,再使用i的值)
++i 子帧运算符
练习题
1,下面的代码会打印什么内容
int i;
for(int i = 0; i < 5; i++)
cout << i;
cout << endl;
结果:01234
2,下面的代码会打印什么内容
int j;
for(j = 0; j < 11; j+=3)
cout << j;
cout << endl << j << endl;
结果:
0369
12
3,下面的代码会打印什么内容
int j = 5;
while(++j<9)
cout << j++ << endl;
结果:
4,下面的代码会打印什么内容
int k = 8;
do
cout << "k = " << k << endl;
while(k++ < 5);
5,编写一个打印 1 2 4 8 16 32 64 的for循环
6,编写一个程序,让用户输入两个整数,输出这两个整数之间(包括这两个整数)所有整数的和。
比如 2 5 里面整数有 2 4 = 6
7,编写一个程序,让用户可以持续输入数字,每次输入数字,报告当前所有输入的和。当用户输入0的时候,程序结束。
#include <iostream>
using namespace std;
int main()
{
//for (int i = 0; i < 5; i++)
// cout << i;
//cout << endl;// 结果:01234
int i = 9;
//++i;
//--i;
//cout << i << endl;
//int result = (i++) + 1; // 结果:10 10
//int result = ++i + 1; // 结果:11 10
//cout << result << " " << i << endl;
//{
// cout << i++ << endl; // 9
// cout << i << endl; // 10
//}
//{
// cout << ++i << endl; // 10
// cout << i << endl; // 10
//}
//{
// cout << i-- << endl; // 9
// cout << i << endl; // 8
//}
{
cout << --i << endl; // 8
cout << i << endl; // 8
}
return 0;
}