Home  >  Article  >  Backend Development  >  What does i+=2 mean in c++

What does i+=2 mean in c++

下次还敢
下次还敢Original
2024-05-01 14:42:131132browse

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.

What does i+=2 mean in c++

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:

  • i: to increment Variable
  • =: compound assignment operator

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What does i-- mean in c++Next article:What does i-- mean in c++