Free Web Tutorials Free Online Tutorials About Us Contact Us

Java Operators Tutorial.


3. Java Increment Decrement Operators


Increment and Decement operator can be used either prefix (--x, ++x) or postfix (x--, x++) mode.

When we write a++ we're using shorthand for a = a + 1. When we say a-- we're using shorthand for a = a - 1.

example expression

equivalent longer expression

a++;(increment)
b=a++;
++a;
b=++a;

a=a+1;
b=a; a=a+1;
a=a+1;
a=a+1; b=a;



Examples:


public class incrementdecrementSample{
	public static void main(String[] args) {
	
	int i;
	for (i=0;i<10;i++)
	{
		System.out.print("increment = " + i);
	}
}


The output from the above examples would be:

	increment = 0
	increment = 1
	increment = 2
	increment = 3
	increment = 4
	increment = 5
	increment = 6
	increment = 7
	increment = 8
	increment = 9
Note i < 10 - so 9 has to be our last number. We could also of said for (i=10;i>0;i--)

free online java tutorial free online java tutorial