在C语言中,使用++i和i++之间的区别是什么,在for循环的增量块中应该使用哪个?


当前回答

预填充是指在同一行上的增量。后增量是指行执行后的增量。

int j = 0;
System.out.println(j); // 0
System.out.println(j++); // 0. post-increment. It means after this line executes j increments.

int k = 0;
System.out.println(k); // 0
System.out.println(++k); // 1. pre increment. It means it increments first and then the line executes

当它带有OR和AND操作符时,它变得更有趣。

int m = 0;
if((m == 0 || m++ == 0) && (m++ == 1)) { // False
    // In the OR condition, if the first line is already true
    // then the compiler doesn't check the rest. It is a
    // technique of compiler optimization
    System.out.println("post-increment " + m);
}

int n = 0;
if((n == 0 || n++ == 0) && (++n == 1)) { // True
    System.out.println("pre-increment " + n); // 1
}

在数组中

System.out.println("In Array");
int[] a = { 55, 11, 15, 20, 25 };
int ii, jj, kk = 1, mm;
ii = ++a[1]; // ii = 12. a[1] = a[1] + 1
System.out.println(a[1]); // 12

jj = a[1]++; // 12
System.out.println(a[1]); // a[1] = 13

mm = a[1]; // 13
System.out.printf("\n%d %d %d\n", ii, jj, mm); // 12, 12, 13

for (int val: a) {
     System.out.print(" " + val); // 55, 13, 15, 20, 25
}

在c++中,指针变量的后/前增量

#include <iostream>
using namespace std;

int main() {

    int x = 10;
    int* p = &x;

    std::cout << "address = " << p <<"\n"; // Prints the address of x
    std::cout << "address = " << p <<"\n"; // Prints (the address of x) + sizeof(int)
    std::cout << "address = " << &x <<"\n"; // Prints the address of x

    std::cout << "address = " << ++&x << "\n"; // Error. The reference can't reassign, because it is fixed (immutable).
}

其他回答

++i:是增量前,另一个是增量后。

i++:获取元素,然后对其进行递增。 ++i:增加i,然后返回元素。

例子:

int i = 0;
printf("i: %d\n", i);
printf("i++: %d\n", i++);
printf("++i: %d\n", ++i);

输出:

i: 0
i++: 0
++i: 2

i++被称为后增量,而++ I被称为前增量。

i++

i++是后增量,因为它在操作结束后将I的值加1。

让我们看看下面的例子:

int i = 1, j;
j = i++;

这里j = 1,但i = 2。在这里,i的值将首先赋给j,然后i将增加。

++i

++i是预增量,因为它在操作之前将i的值加1。 它表示j = i;将在i++之后执行。

让我们看看下面的例子:

int i = 1, j;
j = ++i;

这里j = 2但是i = 2。这里i的值将在i增加i之后赋给j。 类似地,++i将在j=i;之前执行。

对于你的问题,在For循环的增量块中应该使用哪个?答案是,你可以用任何一个…没关系。它将执行相同次数的for循环。

for(i=0; i<5; i++)
   printf("%d ", i);

And

for(i=0; i<5; ++i)
   printf("%d ", i);

两个循环将产生相同的输出。也就是0 1 2 3 4。

重要的是你在哪里使用它。

for(i = 0; i<5;)
    printf("%d ", ++i);

在这种情况下,输出将是1 2 3 4 5。

i++和++ I

这段小代码可能有助于从不同的角度来看待与已经发布的答案的差异:

int i = 10, j = 10;
  
printf ("i is %i \n", i);
printf ("i++ is %i \n", i++);
printf ("i is %i \n\n", i);
  
printf ("j is %i \n", j);
printf ("++j is %i \n", ++j);
printf ("j is %i \n", j);

结果是:

//Remember that the values are i = 10, and j = 10

i is 10 
i++ is 10     //Assigns (print out), then increments
i is 11 

j is 10 
++j is 11    //Increments, then assigns (print out)
j is 11 

注意之前和之后的情况。

for循环

至于在for循环的增量块中应该使用它们中的哪一个,我认为我们能做的最好的决定是使用一个很好的例子:

int i, j;

for (i = 0; i <= 3; i++)
    printf (" > iteration #%i", i);

printf ("\n");

for (j = 0; j <= 3; ++j)
    printf (" > iteration #%i", j);

结果是:

> iteration #0 > iteration #1 > iteration #2 > iteration #3
> iteration #0 > iteration #1 > iteration #2 > iteration #3 

我不知道你怎么想,但我没有看到它的用法有任何不同,至少在for循环中是这样。

我假设你现在已经理解了语义上的差异(尽管说实话我想知道为什么 人们会问“运算符X是什么意思”的问题,而不是阅读, 你知道的,一本书或网络教程之类的。

不管怎样,至于用哪个,忽略性能的问题 即使在c++中也不太重要。这是你做决定时应该遵循的原则 使用哪一种:

用代码表达你的意思。

如果语句中不需要value-before-increment,就不要使用这种形式的操作符。这是一个小问题,但除非你的风格指南禁止这样做 版本完全赞成其他的(又名愚蠢的风格指南),你应该使用 最准确地表达你要做的事情的形式。

QED,使用预增量版本:

for (int i = 0; i != X; ++i) ...

:不久

++i和i++的工作原理是一样的,如果你不在函数中写它们的话。如果你使用function(i++)或function(++i),你可以看到区别。

函数(++i)表示首先将i增加1,然后将i放入具有新值的函数中。

Function (i++)表示在i加1之后将第一个i放入函数中。

int i=4;
printf("%d\n",pow(++i,2));//it prints 25 and i is 5 now
i=4;
printf("%d",pow(i++,2));//it prints 16 i is 5 now