The For Loop¶
(repeats a group of statements a fixed number of times)
for (initialization; test; update) {
statement;
...
statement;
}
Example
for (int i = 1; i <= 10; i++) {
System.out.println(i + " squared is " + (i * i));
}
For Loop Control Flow Diagram¶
Ways to Increment and Decrement the Update Variable¶
Statement | Description |
---|---|
i++ | increment i by 1; same as i = i + 1 |
i += 1 | increment i by 1; same as i = i + 1 |
i-- | decrement i by 1; same as i = i - 1 |
i -= 1 | decrement i by 1; same as i = i - 1 |