English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

When in C ++Using i in ++Or++ i?

The increment operator is used to increase the value by one, and the decrement is the opposite. The decrement operator decreases the value by one.

pre-increment (++ i) -Increment the value before assigning it to a variable1.

post-increment (i ++) -After assigning a value to a variable, the value will increment.

This is C ++i in the language ++and++ the syntax of i

++variable_name; // Pre-increment
variable_name++; // Post-increment

Here,

variable_name-The variable name given by the user.

This is C ++Example of increment and decrement in the language,

Example

#include <iostream>
using namespace std;
int main() {
   int i = 5;
   cout << "The pre-incremented value: " << i;
   while(++i < 10 )
   cout << "\t" << i;
   cout << "\nThe post-incremented value: " << i;
   while(i++ < 15 )
   cout << "\t" << i;
   return 0;
}

Output result

The pre-incremented value: 56789
The post-incremented value: 101112131415

In the above program,main()There is code for incrementing and decrementing before and after in the function. Increment the integer variable i until the value of i is less than10,then increment, until the value of i is less than15.

while(++i < 10 )
printf("%d\t",i);
cout << "\nThe post-incremented value: " << i;
while(i++ < 15 )
printf("%d\t",i);