Skip to content

Instantly share code, notes, and snippets.

@sprytnyk
Last active June 24, 2020 22:27
Show Gist options
  • Save sprytnyk/b01d247d00d9d5059418b17f37e5cf47 to your computer and use it in GitHub Desktop.
Save sprytnyk/b01d247d00d9d5059418b17f37e5cf47 to your computer and use it in GitHub Desktop.
Pre-increment vs post-increment in CPP.
#include <iostream>
using namespace std;
void pre_increment() {
// Pre-increment.
// i is: 1
// j is: 1
// ++i
int i(0);
int j;
j = ++i;
cout << "Pre-increment." << endl;
cout << "i is: " << i << endl;
cout << "j is: " << j << endl;
}
void post_increment() {
// Post-increment.
// i is: 1
// j is: 0
// i++
int i(0);
int j;
j = i++;
cout << "Post-increment." << endl;
cout << "i is: " << i << endl;
cout << "j is: " << j << endl;
cout << "j is: " << j << endl;
}
int main() {
pre_increment();
post_increment();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment