Break and labeled break

As you have probably noticed, the break statement is essential for the selection statements to be able to work (see the 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 live online training, plus books, videos, and digital content from nearly 200 publishers.