24su ver.
Note: this is for the Summer 2024 iteration of CSE 121. Looking for a different quarter? Please visit https://courses.cs.washington.edu/courses/cse121/.
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 |