i =2 is equivalent to i = i 2 in C and is used to increment a variable by a specific increment (2 in this case). It is often used to update variables in loops.
The meaning of i =2 in C
In C, i =2 is a compound assignment operator , equivalent to i = i 2. It adds 2 to the current value of variable i and stores the result back to i.
Usage
i =2 operator is mainly used to increment variables, increasing by 2 for each iteration. This is useful in scenarios where a variable needs to be updated by a specific increment in a loop.
Syntax
i =2 The syntax of the operator is as follows:
<code class="cpp">i += 2;</code>
Among them:
Example
The following example demonstrates the use of the i =2 operator:
<code class="cpp">int main() { int i = 0; while (i < 10) { cout << i << " "; i += 2; } return 0; }</code>
Output:
<code>0 2 4 6 8</code>
In this example, the i =2 operator is used to increment i by 2 in a while loop until it reaches 10.
The above is the detailed content of What does i+=2 mean in c++. For more information, please follow other related articles on the PHP Chinese website!