In C, the difference between the increment operators i and i is the order of execution: i is incremented first and then the expression is evaluated, while i is first evaluated and then incremented. Therefore, use i when you need to use the incremented value immediately, and use i when you need to use the original value first and then increment it.
The difference between i and i in C
In C, i
and i
are both increment operators, but they differ in the order of execution.
i
: Prefix increment
i
will be incremented before calculating the expression . i
i
by 1 and then prints the result: <code class="cpp">int i = 10; cout << ++i << endl; // 输出:11</code>
i
: post-increment
i
. i
i
and then increments it by 1: <code class="cpp">int i = 10; cout << i++ << endl; // 输出:10</code>
Summary
i
Increments i
before evaluating the expression. i
Increments i
after evaluating the expression. i
when you need to increment i
and use its new value immediately. Use i
when you need to use the original value of i
before incrementing it. The above is the detailed content of The difference between ++i and i++ in c++. For more information, please follow other related articles on the PHP Chinese website!