© Ron Dai 2019
R. DaiLearn Java with Mathhttps://doi.org/10.1007/978-1-4842-5209-3_8

8. Loop Structure – for Loop

Ron Dai1 
(1)
Seattle, WA, USA
 
Simply put, the loop structure repeatedly does something until the state is changed (see Figure 8-1).
../images/485723_1_En_8_Chapter/485723_1_En_8_Fig1_HTML.png
Figure 8-1

The for loop structure

Example

Here is an example:
        for (int i = 0; i < 100; i++) {
               <do something>
        }
There are three key elements in the for statement:
  • int i = 0: declare a counter variable and assign an initial value to it;

  • i < 100: define the condition to continue with the for loop; as long as this condition is true the loop will run, when it is not true, we stop and exit from ...

Get Learn Java with Math: Using Fun Projects and Games 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.