增量运算符用于将值增加一,而减量则与增量相反。递减运算符将该值减一。
预递增(++ i) -在将值分配给变量之前,将值递增1。
后递增(i ++) -将值分配给变量后,值将递增。
以下是前后递增的语法。
++variable_name; // Pre-increment variable_name++; // Post-increment
这里,
variable_name-用户给定的变量的任何名称。
这是C ++中前后递增的示例。
#include <iostream>
using namespace std;
int main() {
int i = 5;
cout << "The pre-incremented value: " << i;
while(++i < 10 )
cout<<"\t"<<i;
cout << "\nThe post-incremented value: " << i;
while(i++ < 15 )
cout<<"\t"<<i;
return 0;
}输出结果
The pre-incremented value: 5 6 789 The post-incremented value: 10 1112131415