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


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

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 26-Jun-2020

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements