+ 2
Difference between pre increment and post increment in for loop
Is there is any difference between following two for loops For(int i =0;i<5;i++) System.out.println(i); AND For(int i=0;i<5;++i) System.out.println(i); If any state with description
7 Respostas
+ 15
Muhammad Khairul Amirin Bin Yaacob both will have 01234
output
In loop post - pre increment will not make any difference
+ 13
some useful old threads 👇
https://www.sololearn.com/Discuss/305422/?ref=app
https://www.sololearn.com/Discuss/489143/?ref=app
+ 6
1. for (int i=0;i<5;i++)
output: 01234
2 for (int i=0;i<5;++i)
output: 12345??
+ 1
Gordie nice post. Thank you.
0
Muhammad Khairul Amirin Bin Yaacob That's wrong. The incremention isn't used directly and therefore it doesn't matter if you use ++i or i++. Pre- and postincrement only differs when you pass it to something while incrementing it.
However: it's better to use pre increment, because post increment is more costly.
Pre increment directly returns the incremented value, but post increments need to copy the value in a temporary variable, increment the original and then returns the previous made copy.
0
Question: how would your answer apply to overloaded increment operators for an object? In case the answer is no: would it be bad practice to even implement post increment in a costly class?
0
pre increment increase the number before and then proceed but post increment proceed the value before and then increase