Incrementing with loops is actually so common in programming that Java (like many other programming languages) includes syntax specifically to address this pattern: for loops.

A for loop header is made up of the following three parts, each separated by a semicolon:

  1. The initialization of the loop control variable.
  2. A boolean expression.
  3. An increment or decrement statement.

The opening line might look like this:

for (int i = 0; i < 5; i++) { // code that will run }

In a for loop, an initialization statement is run once in order to initialize the loop control variable. This variable is modified in every iteration, can be referenced in the loop body, and used to test the boolean condition. In the example above, i is the loop control variable.

Let’s breakdown the above example:

  1. i = 0: i is initialized to 0
  2. i < 5: the loop is given a boolean condition that relies on the value of i. The loop will continue to execute until i < 5 is false.
  3. i++: i will increment at the end of each loop and before the condition is re-evaluated.

So the code will run through the loop a total of five times.

We’ll also hear the term “iteration” in reference to loops. When we iterate, it just means that we are repeating the same block of code.


Review the syntax of for loops and click Next when you’re ready to build some yourself!

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?