×

Java Programs

Java Practice

Java program to implement infinite loop using while loop

Using the while loop, we have to implement an infinite loop.
Submitted by Nidhi, on March 08, 2022

Problem statement

In this program, we will use the while loop to print the "Hello" message infinite times.

Source Code

The source code to implement an infinite loop using the while loop is given below. The given program is compiled and executed successfully.

// Java program to implement infinite loop 
// using while loop

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

Output

Hello
Hello
Hello
Hello
Hello
.
.
Infinite time

Explanation

In the above program, we created a public class Main. It contains a static method main().

The main() method is an entry point for the program. Here, we used Boolean value true in the while loop for condition. That's why the while loop will never terminate and printed "Hello" message infinite times.

Java Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.