int counter=0;
while ( counter < 10 )
{
System.out.println("counter is now " + counter );
counter++ ;
}
The increment operator ++
adds one to a variable.
Usually the variable is an integer type (byte, short, int, or long)
but it can be a floating point type (float or double.)
No character is allowed between the two plus signs.
Usually the plus signs are put immediately adjacent to the variable,
as above, although this is not necessary.
The increment operator can be used as part of an arithmetic expression, as in the following:
int sum = 0; int counter = 10; sum = counter++ ; System.out.println("sum: "+ sum " + counter: " + counter );
Here is how the statement sum = counter++;
sum = counter++;
counter
is incremented after
the value it holds has been used.
It is vital to understand the details of this:
=
counter
has not been incremented yet.)=
++
operator works: counter
is incremented to 11.sum: 10 counter: 11