Java program to write an infinite loop using for and while

Write an infinite loop program using while and for loop in Java :

Infinite loop is a loop that never ends. In this tutorial, I will show you how to write an infinite loop in Java using for and while loop.

While loop to write an infinite loop :

‘while’ loop first checks a condition and then runs the code inside its block. We can make it an infinite loop by making the condition ‘true’ :

/*
 * Copyright (C) 2021 codevscolor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */



/**
 * Example Class
 */
public class ExampleClass {

    /**
     * System.out.println utility method
     *
     * @param value : value to print
     */
    static void print(String value) {
        System.out.println(value);
    }


    /**
     * main method for this class
     */
    public static void main(String[] args) {

        while(true){
            print("printing...");
        }

    }
}

It will print the line continuously for an infinite time. You will have to terminate the program to stop the printing :

printing...
printing...
printing...
printing...
printing...
printing...
printing...
.
.
.
.

for loop to write an infinite loop :

We can also use a ’for’ loop to write one infinite loop as like below :

/*
 * Copyright (C) 2021 codevscolor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */



/**
 * Example Class
 */
public class ExampleClass {

    /**
     * System.out.println utility method
     *
     * @param value : value to print
     */
    static void print(String value) {
        System.out.println(value);
    }


    /**
     * main method for this class
     */
    public static void main(String[] args) {

        for(;;){
            print("printing...");
        }

    }
}

Both methods will produce the same infinite result but using the ’while’ loop makes the program more readable.

How to exit an infinite loop :

Suppose you want to run the loop for an infinite time, and each time you are taking an input from the user. The loop will exit only if the user enters a certain value. So, how can we achieve that?

We can use the ’break’ statement to exit a loop. For example :

/*
 * Copyright (C) 2021 codevscolor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import java.util.Scanner;

/**
 * Example Class
 */
public class ExampleClass {

    /**
     * System.out.println utility method
     *
     * @param value : value to print
     */
    static void print(String value) {
        System.out.println(value);
    }


    /**
     * main method for this class
     */
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String value;

        while (true) {
            print("Enter a letter...");
            value = sc.nextLine();

            if (value.equals("q")) {
                break;
            }
        }

    }
}

This loop will take one input from the user each time. If the user gives an input ‘q’ it will exit.

Sample output :

Enter a letter...
r
Enter a letter...
t
Enter a letter...
e
Enter a letter...
q

Process finished with exit code 0

Similar tutorials :