While Loop:

Loops execute the same block of code over and over again until the provided condition is true. We use while loop when we do not know the exactly that how many times loop will run.

whileLoop.java
public class WhileLoopExample {
    public static void main(String[] args) {
        int count = 1;

        while (count <= 5) {
            System.out.println("Count is: " + count);
            count++;
        }
    }
}

Do while Loop:

In do while loop, first iteration is done without checking the condition. After first iteration, condition is checked and if it is true second iteration is done and this continues until the condition is true.


whileLoop.java
public class WhileLoopExample {
    public static void main(String[] args) {
        int count = 1;

        do {
            System.out.println("Count is: " + count);
            count++;
        }while (count <= 5);
    }
}

Infinite Loops:

The loop will be infinite if the condition is true and not becoming false.


whileLoop.java
public class WhileLoopExample {
    public static void main(String[] args) {
        int count = 1;

        do {
            System.out.println("Count is: " + count);
            count++;
        }while (true);
    }
}

Note : You can controll this infinite loop by pressing `Ctrl + c`.