简体   繁体   English

循环中的前增量和后增量有什么区别(for/while)?

[英]What is the difference between pre-increment and post-increment in the cycle (for/while)?

My interest is in the difference between for and while loops.我的兴趣在于forwhile循环之间的区别。 I know that the post-increment value is used and then incremented and the operation returns a constant pre-increment.我知道使用后增量值然后递增,并且操作返回一个常量前增量。

while (true) {
    //...
    i++;
    int j = i;
}

Here, will j contain the old i or the post-incremented i at the end of the loop?在这里, j将在循环结束时包含旧的i还是后递增的i

Since the statement i++ ends at the ;由于语句 i++ 结束于; in your example, it makes no difference whether you use pre- or post-increment.在您的示例中,无论您使用前增量还是后增量都没有区别。

The difference arises when you utilize the result:当您使用结果时会出现差异:

int j = i++; // i will contain i_old + 1, j will contain the i_old.

Vs:对比:

int j = ++i; // i and j will both contain i_old + 1.

Depends on how you use them.取决于你如何使用它们。

  • i++ makes a copy, increases i, and returns the copy (old value). i++创建一个副本,增加 i,并返回副本(旧值)。
  • ++i increases i, and returns i. ++i增加 i,并返回 i。

In your example it is all about speed.在您的示例中,这完全与速度有关。 ++i will be the faster than i++ since it doesn't make a copy. ++i将比i++更快,因为它不会复制。

However a compiler will probably optimize it away since you are not storing the returned value from the increment operator in your example, but this is only possible for fundamental types like a int .但是,编译器可能会将其优化掉,因为您没有在示例中存储增量运算符的返回值,但这仅适用于int等基本类型。

Basic answer for understanding.理解的基本答案。 The incrementation operator works like this:递增运算符的工作方式如下:

// ++i
function pre_increment(i) {
    i += 1;
    return i;
}
// i++
function post_increment(i) {
    copy = i;
    i += 1;
    return copy;
}

A good compiler will automatically replace i++ with ++i when it detect that the returned value will not be used.当一个好的编译器检测到返回的值不会被使用时,它会自动将i++替换为++i

In pre-increment the initial value is first incremented and then used inside the expression.在预增量中,初始值首先递增,然后在表达式中使用。

a = ++i;

In this example suppose the value of variable i is 5. Then value of variable a will be 6 because the value of i gets modified before using it in a expression.在这个例子中,假设变量i的值为 5。那么变量a的值为 6,因为i的值在在表达式中使用之前已被修改。

In post-increment value is first used in a expression and then incremented.在后增量中,值首先在表达式中使用,然后递增。

a = i++;

In this example suppose the value of variable i is 5. Then value of variable a will be 5 because value of i gets incremented only after assigning the value 5 to a .在此示例中,假设变量i的值为 5。那么变量a值将是 5,因为i的值仅在将值 5 分配给a后才会增加。

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argp)
{
    int x = 5;

    printf("x=%d\n", ++x);
    printf("x=%d\n", x++);
    printf("x=%d\n", x);

    return EXIT_SUCCESS;
}

Program Output:程序输出:

x=6
x=6
x=7

In the first printf statement x is incremented before being passed to printf so the value 6 is output, in the second x is passed to printf (so 6 is output) and then incremented and the 3rd printf statement just shows that post increment following the previous statement by outputting x again which now has the value 7.在第一个 printf 语句中,x 在传递给 printf 之前递增,因此输出值 6,在第二个 x 传递给 printf(所以输出 6)然后递增,第三个 printf 语句仅显示前一个后递增通过再次输出 x 现在的值 7 来声明。

i++ 使用 i 的值然后递增它,但 ++i 在使用它之前递增 i 的值。

The difference between post- and pre-increment is really, in many cases subtle.在许多情况下,后增量和前增量之间的差异确实很微妙。 post incremenet, aka num++ , first creates a copy of num , returns it, and after that , increments it. post incremenet,又名num++ ,首先创建num的副本,返回它,然后递增它。 Pre-increment, on the other hand, aka ++num , first evaluates, then returns the value.另一方面,预增量又名++num ,首先计算,然后返回值。 Most modern compilers, when seeing this in a loop, will generally optimize, mostly when post increment is used, and the returned initial value is not used.大多数现代编译器,当在循环中看到这一点时,通常会优化,主要是在使用后增量时,并且不使用返回的初始值。 The most major difference between the 2 increments, where it is really common to make subtle bugs, is when declaring variables, with incremented values: Example below:两个增量之间的最大区别(通常会产生细微的错误)是在声明具有增量值的变量时:示例如下:

int num = 5;
int num2 = ++num; //Here, first num is incremented, 
                  //then made 6, and that value is stored in num2;

Another example:另一个例子:

int num = 5;
int num2 = num++; //Here, num is first returned, (unfortunately?), and then 
                  //incremented. This is useful for some cases.

The last thing here I want to say is BE CAREFUL WITH INCREMENTS .我想说的最后一件事是小心增量 When declaring variables, make sure you use the right increment, or just write the whole thing out ( num2 = num + 1 , which doesn't always work, and is the equivalent of pre-increment).声明变量时,请确保使用正确的增量,或者只写出整个内容( num2 = num + 1 ,这并不总是有效,相​​当于预增量)。 A lot of trouble will be saved, if you use the right increment.如果您使用正确的增量,将会省去很多麻烦。

it does not matter if you use pre or post increment in an independent statement, except for the pre-increment the effect is immediate在独立语句中使用前置增量还是后置增量都没有关系,除了前置增量,效果是即时的

//an example will make it more clear:


int n=1;
printf("%d",n);
printf("%d",++n);// try changing it to n++(you'll get to know what's going on)

n++;
printf("%d",n);

output: 123输出:123

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM