From the course: Oracle Java Certification: 2. Operators and Decision Statements

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Break, continue, and optional labels

Break, continue, and optional labels - Java Tutorial

From the course: Oracle Java Certification: 2. Operators and Decision Statements

Start my 1-month free trial

Break, continue, and optional labels

- [Instructor] Break and continue statements allow us to change the control flow of loops. The break statement will terminate the loop in which the break statement is defined. In other words, when a break statement is executed inside a loop, the control flow will jump to the first statement after the loop. The continue statement on the other hand, aborts the current iteration, but will continue the execution of the loop from the next iteration. When loops are nested, there's no easy way to break out of an outer loop from inside an inner loop. This is when a label comes into play. A label is an identifier followed by a colon that marks the beginning of a loop. From anywhere in the loop, including the nested inner loops, we can use a break or a continue statement with a label to achieve similar effects on outer loops. Let's look at an example. This code uses a double-nested for loop to iterate over a two-dimensional…

Contents