What is the difference between (++c) and (c++)?
Lets say c = 4
I know that for (++c) you would increment increment 4 by 1 so 5, but for (c++)?
What is the difference between (++c) and (c++)?
Lets say c = 4
I know that for (++c) you would increment increment 4 by 1 so 5, but for (c++)?
Both c++ and ++c increment the variable they are applied to. The result returned by c++ is the value of the variable before incrementing, whereas the result returned by ++c is the value of the variable after the increment is applied.
example:
public class IncrementTest{
public static void main(String[] args){
System.out.println("***Post increment test***");
int n = 10;
System.out.println(n); // output 10
System.out.println(n++); // output 10
System.out.println(n); // output 11
System.out.println("***Pre increment test***");
int m = 10;
System.out.println(m); // output 10
System.out.println(++m); // output 11
System.out.println(m); // output 11
}
}
For more info, read this: http://www.javawithus.com/tutorial/increment-and-decrement-operators Or google post increment and pre increment in java.