Skip to content

Break and Continue

Lets look at the small program we made in the while loop module:

    public static void main(String[] args) {
   String input = "";
Scanner scanner = new Scanner(System.in);
       while (!input.equals("quit")){
       System.out.print("Input: ");
       input = scanner.next().toLowerCase();
       System.out.println(input);
       }
    }

This program has a small problem, It has been designed to return either a user’s input or to terminate the program when the user types in quit. At the moment the way the program is written the quit input will echo prior to the program exiting which may not be ideal for the intended function.

One way to solve this problem is to check the input before printing it.

    public static void main(String[] args) {
   String input = "";
Scanner scanner = new Scanner(System.in);
       while (!input.equals("quit")){
       System.out.print("Input: ");
       input = scanner.next().toLowerCase();
       if (!input.equals("quit")){
System.out.println(input);
       }
       }
    }

A break; statement can be used to terminate the loop when a specific condition is met. It immediately exits the loop, ignoring any remaining code within it. Here’s an updated version using break:

    public static void main(String[] args) {
   String input = "";
Scanner scanner = new Scanner(System.in);
       while (!input.equals("quit")){
       System.out.print("Input: ");
       input = scanner.next().toLowerCase();
       if (input.equals("quit")){
break;
       }
System.out.println(input);
       }
    }

The continue; statement moves control to the beginning of the loop, skipping the rest of the current iteration. This can be useful if you want to ignore specific inputs but continue processing the rest of the loop.

    public static void main(String[] args) {
   String input = "";
Scanner scanner = new Scanner(System.in);
       while (!input.equals("quit")){
       System.out.print("Input: ");
       input = scanner.next().toLowerCase();
       if (input.equals("pass")){
       continue;
       }
       if (input.equals("quit")){
break;
       }
System.out.println(input);
       }
    }

Here when a user types in pass the loop is re-executed from the System.out.print("Input: "); line meaning the user’s input will not be echoed in the terminal.

Now that we have updated our break condition we can refactor our code:

    public static void main(String[] args) {
   String input = "";
Scanner scanner = new Scanner(System.in);
       while (true){
       System.out.print("Input: ");
       input = scanner.next().toLowerCase();
       if (input.equals("pass")){
       continue;
       }
       if (input.equals("quit")){
break;
       }
System.out.println(input);
       }
    }

As we are catering to our "quit" condition in the while loop itself we can now set the loop to run infinitely until the user types in quit. When using while loops with a true condition it is important to have break; exit condition that occur when the loop is no longer required to ensure no risk of an infinite loop.