In C and C++ programming language, there is an operator known as an increment operator which is represented by ++. And it is used to increase the value of the variable by 1.
在C和C ++编程语言中,有一个称为递增运算符的运算符 ,由++表示。 它用于将变量的值增加1。
There are two types of Increment operator,
增量运算符有两种,
Pre-increment Operator
预递增运算符
Post-increment Operator
后递增运算符
预递增运算符 (Pre-increment Operator)
The Pre-increment operator increases the value of the variable by 1 before using it in the expression, i.e. the value is incremented before the expression is evaluated.
在表达式中使用变量之前,Pre-increment运算符将变量的值增加1,即,在对表达式求值之前,将值增加。
Syntax:
句法:
++a
Example:
例:
Input:
a = 10;
b = ++a;
Output:
a = 11
b = 11
In the expression b=++a, ++a will be evaluated first thus, the value of a will be 11 and then assignment operation will be performed.
在表达式b = ++ a中 ,将首先对++ a求值,因此a的值为11,然后将执行赋值操作。
Program to demonstrate the example of pre-increment
程序演示预增量示例
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = 0;
cout << "Before the operation..." << endl;
cout << "a: " << a << ", b: " << b << endl;
//Pre-increment operation
b = ++a;
cout << "After the operation..." << endl;
cout << "a: " << a << ", b: " << b << endl;
return 0;
}
Output:
输出:
Before the operation...
a: 10, b: 0
After the operation...
a: 11, b: 11
后递增运算符 (Post-increment Operator)
The Post-increment operator increases the value of the variable by 1 after using it in the expression, i.e. the value is incremented after the expression is evaluated.
在表达式中使用变量后,Post-increment运算符会将变量的值增加1,即,对表达式求值后,该值将递增。
Syntax:
句法:
a++
Example:
例:
Input:
a = 10;
b = a++;
Output:
a = 11
b = 10
In the expression b=a++, a++ will be evaluated after assigning the value of a to the b. Thus, the value of b will be 10 and then a++ will be evaluated and then the value of a will be 11.
在表达式B = A ++,++ 一个将a的值分配给B之后进行评价。 因此,b的值将是10,然后一个++将被评估,然后a的值将是11。
Program to demonstrate the example of post-increment
演示后增量示例的程序
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = 0;
cout << "Before the operation..." << endl;
cout << "a: " << a << ", b: " << b << endl;
//Pre-increment operation
b = a++;
cout << "After the operation..." << endl;
cout << "a: " << a << ", b: " << b << endl;
return 0;
}
Output:
输出:
Before the operation...
a: 10, b: 0
After the operation...
a: 11, b: 10
预增和后增运算符之间的区别 (Difference between pre-increment and post-increment operators)
Since both are used to increase the value of the variable by 1. But based on the above discussion and examples, the difference between pre-increment and post-increment operators is very simple. The pre-increment increased the value before the expression is evaluated and the post-increment increases the value after the expression is evaluated.
由于两者都用于将变量的值增加1。但是基于上面的讨论和示例,预增和后增运算符之间的区别非常简单。 增量前增加了对表达式进行评估前的值,增量后增加了对表达式进行评估后的值。
Program to demonstrate the use of pre and post increment operators
演示使用前后递增运算符的程序
#include <iostream>
using namespace std;
int main()
{
int A = 10, B = 20, C = 0;
C = A++ + ++B * 10 + B++;
cout << A << "," << B << "," << C;
return 0;
}
Output:
输出:
11, 22, 241
Explanation:
说明:
Based on the above discussion, the express will be evaluated like this,
根据以上讨论,快递将像这样进行评估,
Values are,
A = 10
B = 20
C = 0
Expression is,
C = A++ + ++B * 10 + B++;
C = 10 + 21 * 10 + 21
= 10 +210 +21
= 241
Finally, the values of A, B and C will be,
A = 11
B = 22
C = 241
Note: Compiler dependency may be there.
注意:可能存在编译器依赖性。
Recommended posts
推荐的帖子
What is the value of sizeof('x') and type of character literals in C++?
翻译自: https://www.includehelp.com/cpp-tutorial/pre-increment-and-post-increment-operators-in-c-cpp.aspx