Loading...

Loops in Java: Doing Things Again and Again

A Simple Thought

Imagine you are asked to clap ten times. You wouldn’t write “clap” ten times on paper, you would just repeat the action ten times.

Computers can do the same thing. Instead of writing the same command over and over, you can tell them to repeat it automatically using loops.

In this lesson, you will learn how to use loops to repeat actions in Java. By the end, you’ll create a small program that counts from 1 to 10, step by step, without writing ten separate print statements.

The Project: Counting Machine

You are building a simple counting program. When it runs, it should display numbers from 1 to 10 in order.
The output will look like this:

text
14 lines
|
37/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
========================
   COUNTING MACHINE
   Counting from 1 to 10
   1
   2
   3
   4
   5
   6
   7
   8
   9
   10
========================

You will make Java repeat the print statement ten times using a loop.

The Concept: The For Loop

A loop is a structure that repeats a block of code automatically as long as a certain condition is true.

The most common type of loop is the for loop. It looks like this:

java
3 lines
|
15/ 500 tokens
1
2
3
for (int i = 1; i <= 10; i++) {
    System.out.println(i);
}
Code Tools

Here’s what happens step by step:

  • int i = 1 sets the starting point of the counter.
  • i <= 10 tells Java to keep running as long as i is less than or equal to 10.
  • i++ increases i by one each time the loop runs.
  • System.out.println(i) prints the current value of i.

After i reaches 10, the loop stops automatically.

Why This Matters

Loops are one of the most powerful tools in programming. They save time, reduce mistakes, and make your code shorter.

Think about any situation where something repeats:

  • Showing a list of products in a shop.
  • Displaying all exam marks for a class.
  • Printing a monthly report for every employee.

Instead of writing dozens of print statements, a loop can do all the work in seconds.

Let’s Build the Counting Program

Here’s the complete code for your project:

java
14 lines
|
100/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class CountingMachine {
    public static void main(String[] args) {

        System.out.println("========================");
        System.out.println("   COUNTING MACHINE");
        System.out.println("   Counting from 1 to 10");

        for (int i = 1; i <= 10; i++) {
            System.out.println("   " + i);
        }

        System.out.println("========================");
    }
}
Code Tools

When you run this program, it will print the numbers 1 through 10, each on a new line.
You only needed one print statement to make it happen.

How It Works

The for loop runs ten times. Each time, the variable i increases by one.
Inside the loop, the print command runs again and again with the new value of i.
Once i becomes 11, the condition i <= 10 becomes false, and the loop stops.

This is how Java can perform tasks automatically without repeating code manually.

Learn Together with AI

If you are using the AI Copilot, you can describe what you want to build in plain English and it will generate the code for you.

Try this prompt:
“Write a Java program that prints numbers from 1 to 10 using a for loop.”

Once you have the code, ask it to modify it.
You could ask:

  • “Change the loop to count down from 10 to 1.”
  • “Print only even numbers between 1 and 20.”
  • “Add a message after every number saying Done.”

This helps you learn how loops can be customized easily.

Practice Challenge

Now try building your own program that prints the table of 5, from 1×5 to 10×5.
The output should look like this:

text
14 lines
|
57/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
========================
   MULTIPLICATION TABLE
   Table of 5
   5 x 1 = 5
   5 x 2 = 10
   5 x 3 = 15
   5 x 4 = 20
   5 x 5 = 25
   5 x 6 = 30
   5 x 7 = 35
   5 x 8 = 40
   5 x 9 = 45
   5 x 10 = 50
========================

Example Solution

java
16 lines
|
117/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class MultiplicationTable {
    public static void main(String[] args) {

        int number = 5;

        System.out.println("========================");
        System.out.println("   MULTIPLICATION TABLE");
        System.out.println("   Table of " + number);

        for (int i = 1; i <= 10; i++) {
            System.out.println("   " + number + " x " + i + " = " + (number * i));
        }

        System.out.println("========================");
    }
}
Code Tools

When you run it, Java will automatically calculate and print each step of the table.
You’ve now built a small math machine that can multiply, count, and repeat without any extra typing.

Coming Up Next

You’ve learned how to make Java repeat actions.
Next, you’ll see how to repeat tasks only while a condition is true using the while loop.
This will help you create programs that wait for input, check passwords, or keep running until the user is done.