Java Reference
In-Depth Information
/*execute these statements*/
break outer; //break with label
}
}
The following Try It Out exercise gives you some practice with the break keyword, both with and
without labels.
Breaking a for Loop
try it out
 Follow these steps to create three sets of nested for loops. The first is a standard for loop, the second
has a break statement added, and the last one includes the break statement with a label.
1. Create a new class named BreakLoop , following the same process. You can continue to use the
same Chapter5 project. Create a new class by right-clicking on the src folder in your project.
Select New and then Class.
2. In the Name field, enter the name of your class, BreakLoop, beginning with a capital letter by
Java convention. In the bottom portion of the New Java Class window, there is a section that
reads: “Which method stubs would you like to create?” You may choose to check the box next to
"public static void main(String[] args)" to automatically create a main method.
3. You should automatically have the basis for the class body shown here:
public class BreakLoop {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
If not, you can type it yourself. You do not need the comments, which are denoted with /** or
// . In Eclipse, they will appear as blue or green text. Comments are useful for explaining what
the code is doing, but are never compiled or executed by Java.
public class BreakLoop {
public static void main(String[] args){
}
}
4. Recall that the main method provides the starting point and ordering for the execution of your pro-
gram. For this exercise, all the statements you want to execute will be inside the main method.
5. For the first nested for loop section, you use the standard syntax you saw earlier in the chapter.
Begin with a print statement to display the area of the program that's being executed. In each itera-
tion of the nested loops, you will simply print the values for x and y , the variables you are using as
iterators. Type the first nested for loop as follows:
Search WWH ::




Custom Search