Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
for (int i = 0; i < 8; i++) { System.out.println(i); } System.out.println("done");
A while loop has only the boolean test; it doesn’t have a built-in initialization or iteration expression. A while loop is good when you don’t know how many times to loop and just want to keep going while some condition is true. But if you know how many times to loop (e.g. the length of an array, 7 times, etc.), a for loop is cleaner. Here’s the loop above rewritten using while:
output:
|
++ -- Pre and Post Increment/Decrement Operator The shortcut for adding or subtracting 1 from a variable.
is the same as:
They both mean the same thing in this context: “add 1 to the current value of x” or “increment x by 1” And: |