Skip to Content
Learn
Learn Java: Loops
For Loops

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 brings together the following steps in a single line, separated by semicolons:

  1. Initializing a counter variable.
  2. Defining the looping condition.
  3. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) { // code that will run }
  1. i = 0: i is initialized to 0.
  2. i < 5: the loop is given a condition that relies on the value of i.
  3. i++: i will increment at the end of each loop.

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

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

Instructions

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

Folder Icon

Take this course for free

Already have an account?