Java Break Statement

The break is a keyword in Java. The break statement plays an important role in the switch statement. When working with loops sometimes we want to terminate the loop and don’t want it to go continue inside the loop, in this scenario we can use the break statement to terminate the loop. Each break statement terminates the enclosing switch statement/loops. The control flow continues with the first statement following the switch block.

We can use the unlabeled break to terminate a switch, for, while or do while loop.

Java Break Statement

Here is an example to break while loop.

Syntax

while ( condition ) //loop starts here
{
if(contains something to process){
do something
break if condition and while loop // while loop will be terminated as soon as it finds 
}	}

import java.util.ArrayList;
import java.util.List;

public class mytest{

	public static void main(String args[]) {
		// program to find a number in list
		// Stop the program when number finds

		int search = 27;
		List list = new ArrayList();
		list.add(1);
		list.add(23);
		list.add(5);
		list.add(10);
		list.add(12);
		list.add(27);
		list.add(11);
		list.add(15);

		int size = list.size() - 1;
		while (size > 0) {
			
			if (list.get(size) == search) {
				System.out.println("number found");
				break;
			}
System.out.println("number = " + list.get(size));

			size--;
		}
	}

}
Example Output

The syntax of a break is a single statement inside any loop

Syntax

jump-statement;    
break;   

Java Break Statement

public class mybreakstatement {

   public static void main(String args[]) {
      int [] numbers = {50, 60, 70, 80, 90, 100,110, 120, 130, 140, 150};

      for(int d : numbers ) {
         if( d == 120 ) {
            break;
         }
         System.out.print( d );
         System.out.print("\n");
      }
   }
}
Example Output

Share