Chapter 3.3.3 – break and continue | Introduction to Programming Using Java

Chapter 3.3.3 – break and continue | Introduction to Programming Using Java

 

3.3.3 break and continue

 

The syntax of the while and do..while loops allows you to test the continuation condition at either the beginning of a loop or at the end. Sometimes, it is more natural to have the test in the middle of the loop, or to have several tests at different places in the same loop. Java provides a general method for breaking out of the middle of any loop. It’s called the break statement, which takes the form break;

 

break and continue

 

When the computer executes a break statement in a loop, it will immediately jump out of the loop. It then continues on to whatever follows the loop in the program. Consider for example:

 

Chapter 3.3.3 - break and continue | Introduction to Programming Using Java

 

If the number entered by the user is greater than zero, the break statement will be executed and the computer will jump out of the loop. Otherwise, the computer will print out “Your answer must be > 0.” and will jump back to the start of the loop to read another input value.

(The first line of this loop, “while (true)” might look a bit strange, but it’s perfectly legitimate. The condition in a while loop can be any boolean-valued expression. The computer evaluates this expression and checks whether the value is true or false. The boolean literal “true” is just a boolean expression that always evaluates to true. So “while (true)” can be used to write an infinite loop, or one that will be terminated by a break statement.)

A break statement terminates the loop that immediately encloses the break statement. It is possible to have nested loops, where one loop statement is contained inside another. If you use a break statement inside a nested loop, it will only break out of that loop, not out of the loop that contains the nested loop. There is something called a labeled break statement that allows you to specify which loop you want to break. This is not very common, so I will go over it quickly. Labels work like this: You can put a label in front of any loop.

A label consists of a simple identifier followed by a colon. For example, a while with a label might look like “mainloop: while…”. Inside this loop you can use the labeled break statement “break mainloop;” to break out of the labeled loop. For example, here is a code segment that checks whether two strings, s1 and s2, have a character in common. If a common character is found, the value of the flag variable nothingInCommon is set to false, and a labeled break is is used to end the processing at that point:

 

Chapter 3.3.3 - break and continue | Introduction to Programming Using Java

 

The continue statement is related to break, but less commonly used. A continue statement tells the computer to skip the rest of the current iteration of the loop.

However, instead of jumping out of the loop altogether, it jumps back to the beginning of the loop and continues with the next iteration (including evaluating the loop’s continuation condition to see whether any further iterations are required). As with break, when a continue is in a nested loop, it will continue the loop that directly contains it; a “labeled continue” can be used to continue the containing loop instead.

 

Chapter 3.3.3 - break and continue | Introduction to Programming Using Java

 

break and continue can be used in while loops and do..while loops. They can also be used in for loops, which are covered in the next section. In Section 3.6, we’ll see that break can also be used to break out of a switch statement.

A break can occur inside an if statement, but in that case, it does not mean to break out of the if. Instead, it breaks out of the loop or switch statement that contains the if statement. If the if statement is not contained inside a loop or switch, then the if statement cannot legally contain a break. A similar consideration applies to continue statements inside ifs.

 

 

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

Chapter 2 – Names and Things | Introduction to Programming Using Java

Chapter 2.5 – Details of Expressions | Introduction to Programming Using Java

Chapter 2.5.1 – Arithmetic Operators | Introduction to Programming Using Java

Chapter 2.5.3 – Relational Operators | Introduction to Programming Using Java

Chapter 2.6.2 – Command Line Environment | Introduction to Programming Using Java

Chapter 2.6.3 – IDEs and Eclipse | Introduction to Programming Using Java

Chapter 3.1.3 – The Basic If Statement | Introduction to Programming Using Java

1 thought on “Chapter 3.3.3 – break and continue | Introduction to Programming Using Java”

Leave a Comment