Blog literacki, portal erotyczny - seks i humor nie z tej ziemi
Program 5.7: The Decrement Operator in a for Loop
++ is called the "increment operator." There is a corresponding decrement operator, --. It works like this.
//Count to ten??
class BuggyCountToTen {
public static void main (String args[]) {
for (int i=1; i <= 10; i = i--) {
System.out.println(i);
}
System.out.println("All done!");
}
}
i++ is shorthand for i = i + 1. Similarly i-- is shorthand for i = i - 1. Adding and subtracting one from a number are such common operations that these special increment and decrement operators have been added to the language. Increment and decrement operators also allow the compiler to be smarter about certain optimizations on some CPU architectures, but mainly they make code easier to write and read.
Copyright 1996