Increment Operators

Revision en2, by slow_hare, 2021-02-19 18:14:20

Could anyone please explain how these expressions are evaluated?

#include <bits/stdc++.h>
using namespace std;

int main()
{
  int a = 1;
  a = a++ + a++;
  cout<<a<<endl;
  a = 1;
  a = ++a + ++a;
  cout<<a<<endl;
  a = 1;
  a = a++ + ++a;
  cout<<a<<endl;
  a = 1;
  a = ++a + a++;
  cout<<a<<endl;
  
  return 0;
}

OUTPUT:

3

6

4

5

Tags #basic math, #operator

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English slow_hare 2021-02-19 18:14:20 16 Tiny change: 'nOUTPUT:\n~~~~~\n3\n6\n4\n5\n~~~~~\n\n' -> 'nOUTPUT:\n3\n\n6\n\n4\n\n5\n\n'
en1 English slow_hare 2021-02-19 18:12:36 415 Initial revision (published)