C / C ++中的预增和后增运算符

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,

增量运算符有两种,

  1. Pre-increment Operator

    预递增运算符

  2. 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.

注意:可能存在编译器依赖性。

翻译自: https://www.includehelp.com/cpp-tutorial/pre-increment-and-post-increment-operators-in-c-cpp.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值