Pre-increment and Post-increment concept in C/C++?

CC++Server Side Programming

Increment operators are used to increase the value by one while decrement works opposite increment. Decrement operator decrease the value by one.

Pre-increment (++i) − Before assigning the value to the variable, the value is incremented by one.

Post-increment (i++) − After assigning the value to the variable, the value is incremented.

The following is the syntax of pre and post increment.

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

Here,

variable_name − Any name of the variable given by user.

Here is an example of pre and post increment in C++.

Example

 Live Demo

#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

The pre-incremented value: 5 6 789
The post-incremented value: 10 1112131415
raja
Published on 08-Nov-2018 06:09:38
Advertisements