Types of Loops in Java
The for loop makes it possible to specify precisely how many times to perform a task in a Java application. Using the break statement makes it possible to stop performing a task when conditions aren’t right, and using the continue statement makes it possible to ignore just one loop (a single value) and then continue processing the next task.
Using the break statement in Java
There are times when you want to stop a for loop early. For example, you might detect a condition that would cause the loop to fail. Perhaps there aren’t enough items to process, or your application detects some other issue. In this case, you can use the break statement to stop the for loop from doing any more work.
Using the continue statement in Java
A simple version of the for loop performs a specific number of loops and another version stops at a specific point using a break statement. A third version of the for loop performs a specific number of loops, but it skips some of the loops when conditions don’t warrant performing the task.
For example, the data that you need to process might not be of the right type or might be missing completely. This third form relies on the continue statement. The continue statement tells the for loop to go immediately to the next loop, rather than complete the current loop.
Just because some code examples look at the break and continue statements separately, that doesn’t mean you can’t combine them in a single for loop. The break and continue statements can appear wherever and whenever you need them.
For example, you could choose to process part of a for loop and then continue on to the next loop if things aren’t working out as expected. If an error occurs in the same for loop, you could choose to use a break statement to end it. A single for loop can also contain multiple instances of both the break and continue statements.
The best way to understand the difference between the break and continue statements is to contrast the output they provide.
Nesting for loops
Sometimes you need to process something using multiple loops. For example, when working with tabular data, you might use one loop to process the rows and another loop to process the columns. There are multiple columns for each row, so the Columns loop appears within the Rows loop. Placing one repeating loop within another is called nesting the loops.
Each iteration of the main loop executes the entire subordinate loop. So, when you start processing the first row, it executes all the column tasks for that row before moving to the next row.
Nesting is the process of enclosing one structure within another of the same type. Java uses nesting in a number of ways, so you’ll see this term used quite often. When working with structures, one structure acts as a container to hold the other structure.
The container structure is called the main, or parent, structure. The structure within the main structure is called the subordinate, or child, structure.
The multiplication tables are one of the better ways to demonstrate nesting because you need to create a loop for rows and another for columns. In addition, you need to create the headings that show the numbers being multiplied, which means using an additional loop.