Skip to main content
Library homepage
 
Engineering LibreTexts

15.7: Break and Continue

  • Page ID
    18893
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Sometimes neither a pretest nor a posttest loop will provide exactly what you need. In the previous example, the “test” needed to happen in the middle of the loop. As a result, we used a flag variable and a nested if-else statement.

    A simpler way to solve this problem is to use a break statement. When a program reaches a break statement, it exits the current loop.

    Scanner in = new Scanner(System.in);
    while (true) {
        System.out.print("Enter a number: ");
        if (in.hasNextDouble()) {
            break;
        }
        String word = in.next();
        System.err.println(word + " is not a number");
    }
    double x = in.nextDouble();
    

    Using true as a conditional in a while loop is an idiom that means “loop forever”, or in this case “loop until you get to a break statement.”

    In addition to the break statement, which exits the loop, Java provides a continue statement that moves on to the next iteration. For example, the following code reads integers from the keyboard and computes a running total. The continue statement causes the program to skip over any negative values.

    Scanner in = new Scanner(System.in);
    int x = -1;
    int sum = 0;
    while (x != 0) {
        x = in.nextInt();
        if (x <= 0) {
            continue;
        }
        System.out.println("Adding " + x);
        sum += x;
    }
    

    Although break and continue statements give you more control of the loop execution, they can make code difficult to understand and debug. Use them sparingly.


    This page titled 15.7: Break and Continue is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) .

    • Was this article helpful?