What is post-increment and pre-increment do, and how do each affect this code. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 3

What is post-increment and pre-increment do, and how do each affect this code.

/don't tell me to read the other discuss questions, because this is my question int num = 0; while(++num < 6) Console.WriteLine(num); or int num = 0; while(++num < 6) Console.WriteLine(num);

2nd Feb 2017, 5:31 AM
Christopher
3 Answers
+ 5
umm both those are the same... pre and post fix operators arent as complicated as they seem.. if the operator is before the variable (ie ++a: prefix) the operation is done before the expression they are included in is evaluated. if the operator is after (a++) the variable it is applied after the expression is evaluated. This question is asked repeatedly every day. this is why you will get "omg search the q&a" as the answer when asking about it.
2nd Feb 2017, 5:42 AM
jay
jay - avatar
+ 2
Consider this 2 examples: int a=2; int b=++a; Here a is incremented before its value is assigned to b, so a=3 and b=3; int a=2; int b=a++; And here the value of a is first assigned to b and then a is incremented so b=2 and a=3 This is the example from one of the courses.
2nd Feb 2017, 5:44 AM
Nikolai Russev
Nikolai Russev - avatar
0
both post and per increment increase the operand value by 1, difference is in their preference when it comes to operator precedence. pre-increment is most prior post-increment is least prior
2nd Feb 2017, 5:53 AM
POJO
POJO - avatar