My Java teacher said it was better to use ++n instead of n++, I am not seeing the logic behind this. Does anyone know?
Interesting example that illustrates the difference:
public class Main {
public static void main(String[] args) {
for (int i=0; i<5; /*not incrementing here*/ )
System.out.println(i++);
}
}
output: 0 1 2 3 4
public class Main {
public static void main(String[] args) {
for (int i=0; i<5; /*not incrementing here*/ )
System.out.println(++i);
}
}
output: 1 2 3 4 5
Notice that neither for-loop performs the incrementation since it does not matter there.