Dremendo Tag Line

Loops in Java Programming

Loops in Java

In this lesson, we will learn about the loops in Java programming and name of the 3 different types of loops.

What is Loop

Loop in Java programming come into use when we need to repeatedly execute a block of statements. For example: Suppose we want to print Hello World 10 times on the screen. This can be done in two ways as shown below:

Iterative Method

In Iterative method we have to write the System.out.println() statement 10 times in our program as shown below.

Example

public class Example
{
    public static void main(String args[])
    {
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
    }
}

Output

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

Here you can see that we have written Hello World 10 times using System.out.println() statement. So, this will print Hello World 10 times on the screen. But if want to print Hello World 100 times on the screen then the iterative method is not suitable for this. To do this we have to use loop in our program.

Loop Method

In the Loop method, we do not have to write the Hello World 10 times in our program rather we have to write Hello World only once and the loop will execute the statement 10 times.

Types of Loop in Java

There are 3 types of loop in Java and they are:

  • for Loop
  • while Loop
  • do while Loop

In our next lessons we will learn more about these loops in great detail with examples.

video-poster