Last active
June 24, 2020 22:27
-
-
Save sprytnyk/b01d247d00d9d5059418b17f37e5cf47 to your computer and use it in GitHub Desktop.
Pre-increment vs post-increment in CPP.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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