0
clear cut difference between increament and decreament ? Eg:difference between a++ and ++a?
2 Answers
+ 15
This is asked a lot lol
++a is a preincrement, and a++ is a postincrement.
--a is a predecrement, and a-- is a postdecrement.
The difference between pre and post is when the ++ or -- kicks in. Pre has very high precedence/priority (it works first), and post had very low priority (works last).
Ex:
b = ++a //Adds 1 to a first, then assigns a to b.
b = a++ //Assigns a to b, then adds 1 to a.
+ 2
Ok so many guys confuse about it.
For e.g.
int a = 1;
int b = 1;
let say
++a means (+1)a, it means it will add +1 to 'a' first so currently your value is 1, but ++a means (+1) to a, so the value becomes 2;
a++ means a(+1) it means it will add +1 to 'a' later, so your current value is 1 but a++ means you are still 1 but later when you reuse the variable, the value becomes 2.
int num1 = 1;
int num2 = 1;
Console.Println(num1); // it outputs 1
++num1; // It added (+1) to num1 first so it becomes 2.
Console.Println(num1); // it outputs 2
num1++; // It added (+1) to num1 later, so the value is still 2.
Console.Println(num1); // it becomes 3 now because it added +1 to the current value now.