Break and labeled break

As you have probably noticed, the break statement is essential for the switch...case selection statements to be able to work (see the switch...case section for more information). If included in the execution block of an iteration statement, it causes the for or while statement to terminate immediately. 

It is widely used in iteration statements while searching for a particular element in an array or collection. To demonstrate how it works, let's assume, for example, that we need to find a certain person by age and name among the students and teachers of a community college. Let's first create the classes Person, Student, and Teacher:

class Person{  private int age;  private  String name;  public Person(int age, String ...

Get Introduction to Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.