ADVERTISEMENT
ADVERTISEMENT

Java program to implement infinite loop using do-while loop

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

Problem Solution:

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

Program/Source Code:

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

// Java program to implement infinite loop using 
// the do-while loop

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

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 loop condition. That's why the do-while loop will never terminate and printed "Hello" message infinite times.

Java Basic Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.