Course Content‎ > ‎Session 3‎ > ‎

Break and Continue

Loops perform a set of operations repeatedly, until the control variable fails to satisfy the test condition. But sometimes, when loop is executing, it is required to skip a part of the loop or to leave the loop, as soon as a certain condition occurs. For example while searching for a particular name in a list of 100 names, for loop is set for 100 names, but as soon as the name we are looking for is encountered, the loop should be terminated immediately. In this case, we need to jump out of the loop immediately. For jumping out of the loop, we use the break statement.  


break statement permits immediate exit from a loop and the program continues with the statement  immediately following the loop. When the loops are nested, the break would only exit from the loop containing it.


continue statement is used to continue with the next iteration, after skipping a part of the loop body. The continue statement simply tells the compiler: skip the following statements and continue with the next iteration. 

Given below is a program, which shows how break and continue are used in loops:

    class demo
        public static void main(String args[])
            int count = 0; 
            /* test condition for the while loop iscount < 1000 */
            while (count < 1000) 
                System.out.println("Count is "+count); 
                /* value of count is displayed */  
                count = count + 2;
                /* value of count is incremented by 2 */
                if(count < 90)
                    /* if count is less than 90, skip rest of the loop 
                       body and continue with next iteration */
                    /* if count is greater than or equal to 90, exit out
                       of the loop and continue with the statement 
                       immediately after the loop(no statement in this case */