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

What does i+=2 mean in c++

下次还敢
Release: 2024-05-01 14:42:13
Original
1212 people have browsed it

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>
Copy after login

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>
Copy after login

Output:

<code>0 2 4 6 8</code>
Copy after login

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!

Related labels:
c++
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template