1

I have researched while(true) loops for past hour but I was unable to find my answers about this loop.

public class Test {
    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
        System.out.println("Enter Integer: ");
        int i = GetAnInteger();
        System.out.println("You entered: " + i);
    }

    public static int GetAnInteger() {
        while (true) {
            try {
                return sc.nextInt();
            }

            catch (InputMismatchException e) {
                sc.next();
                System.out.println("That's not an Integer, try again: ");
            }
        }
    }
}

Question 1: We know all the statements in the code will run by the compiler. The purpose of 'while(true)' is to make sure the code runs and it runs indefinitely, the code in the method WILL be executed so why do we need a while(true) loop in the first place?

Question 2: If I remove the 'while(true)' statement, the IDE asks me to create a return statement or make the method as void, why? How does 'while(true)' work in this scenario?

Other posts on Stack Overflow were mostly debating why 'while(true)' is bad or good, I am not interested in this. I am interested in why this code breaks down without 'while(true)' and how do I know when to use 'while(true)' in my other codes?

  • I searched youtube, java complete reference and stack overflow for past half an hour but couldnt find any answers. This code was taken from "Java for dummies" book and it runs away from explaining the purpose of this while(true) statement.
zappee
  • 20,148
  • 14
  • 73
  • 129
Anurag
  • 19
  • 2
  • `while(true) {}` is an anti-pattern, it is an endless loop, if you see it, there is always better way of doing it as is the case here as evidenced in the correct way to use the `Scanner` as explained in the duplicate. –  Sep 04 '18 at 23:59

2 Answers2

1
  1. The purpose of the while(true) is so that it will run until the input is valid and it reaches the return statement. When it reaches the return statement then it will exit the method/loop

  2. This is because there is no guarantee that the try section will be executed. If it isn't then the method needs something to return. You must include a return statement for every possible branch of your method. Consider the following:

    try {
         //Oh no! InputMismatchException!!
         return sc.nextInt(); 
         //Goes to catch block
    } catch (InputMismatchException e) {
         sc.next();
         System.out.println("That's not an Integer, try again: ");
         //executes catch block
    }
    //Uh oh. Now what? What gets returned?
    
GBlodgett
  • 12,704
  • 4
  • 31
  • 45
0

so why do we need a while(true) loop in the first place?

The while loop allows the user to "try again" if they enter something that isn't an integer. When I run your program, I can do this:

Enter Integer: 
this
That's not an Integer, try again: 
is
That's not an Integer, try again: 
not
That's not an Integer, try again: 
an
That's not an Integer, try again: 
integer
That's not an Integer, try again: 
1
You entered: 1

If I remove the 'while(true)' statement, the IDE asks me to create a return statement or make the method as void, why? How does while(true) work in this scenario?

With the while loop in place, you're guaranteed to eventually return something. Without the while loop, you don't have anything to return if the user doesn't enter an integer. So you have to add something at the end of the method (a default return value, which breaks the intent of the program), or you have to make the method not have a return value at all.

Let's imagine that we delete the while loop, and we add return 0 at the end of the method to make the compiler happy. Now let's try the above scenario again:

Enter Integer: 
this
That's not an Integer, try again: 
You entered: 0

Obviously this is not what we want!

Ben P.
  • 52,661
  • 6
  • 95
  • 123