C.15 for Repetition Statement

Java also provides the for repetition statement, which specifies the counter-controlled-repetition details in a single line of code. Figure C.13 reimplements the application of Fig. C.12 using for.

Fig. C.13 Counter-controlled repetition with the for repetition statement

Computer code has 15 lines plus output.

When the for statement (lines 10–11) begins executing, the control variable counter is declared and initialized to 1. Next, the program checks the loop-continuation condition, counter <= 10, which is between the two required semicolons. Because the initial value of counter is 1, the condition initially is true. Therefore, ...

Get Android How to Program, 3/e now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.