Iteration allows you to repeat a sequence of steps. Java provides three different loops the allow you to implement an iterative algorithm: for loops, while loops, and do while loops.
The keyword for is followed by parentheses. Inside of the parentheses is the initialization of the control variable followed by a semi-colon, the condition followed by a semi-colon, and the update of the control variable. The body of the loop is surrounded by curly braces. A common for loop might look as follows:
The control variable is initialized before the loop, the condition is in parentheses following the while keyword, and the body of the loop is surrounded by curly braces. The body of the loop must contain the update of the control variable. Failing to update the control variable results in an infinite loop.
A while loop can be translated to a for loop and vice versa. Below are two examples of while loops: the first is identical to the for loop above and the second is a better example of a while loop that executes until a sentinel value is entered.
Because you know the body of the loop will execute at least once, you can initialize your control variable in the same place where you update it.