Next: Observations on the for
Up: Unit 06
Previous: Loop controlled by a
for statement
Syntax:
for (initialization; condition; update)
statement
- initialization is an expression with side-effect that
initializes a control variable (typically an assignment), which can also be a
declaration with initialization
- condition is an expression of type boolean
- update is an expression with side-effect that typically
consists in updating (i.e., incrementing or decrementing) the control
variable
- statement is a single statement (also called
body of the for loop)
Semantics: is equivalent to
{
initialization;
while (condition) {
statement
update;
}
}
(There is an exception in the case of the continue statement, which
needs to be translated in a more complicated way.)
Example: Print out 100 stars.
for (int i = 0; i < 100; i++)
System.out.print("*");
Next: Observations on the for
Up: Unit 06
Previous: Loop controlled by a