Quick Summary of Loops in Java

By John Paul Mueller

Creating for loops allows a Java application to perform a task a specific number of times. Creating while loops allows an application to perform a task until it has been completed, and for-each loops allows an application to perform tasks with collections of objects until every object in the collection has been used.

Here are the key points you should remember about loops in Java:

  • The for loop repeats a series of tasks a specific number of times.

  • Use the break statement to stop a for loop from continuing to process tasks.

  • Use the continue statement when you want to stop a single for loop from processing but want to continue with the remaining loops.

  • Nest two or more sets of for loops when you need to process multiple dimensions or create a complex processing loop series.

  • A while loop continues to repeat a series of tasks until a specific condition is met.

  • The do…while statement acts much like the while statement, but it has the distinction of executing the tasks within its structure at least once, no matter what condition you might set for it.

  • It’s possible to repeat most forms of standard looping requirements by using the for, while, or do…while statements and your choice depends on convenience or personal taste.

  • The for-next loop is special in that it works with each element in an enumeration, array, or collection.

  • The for-each loop relies on a special kind of for loop to do its work.

  • You use the for-each loop to process individual elements of a collection of elements one element at a time.

  • Using an enumeration can make it easier to work with your application.

And here are some tech terms you should know when working with loops in Java:

  • counter variable: A special sort of variable used to track the current count of something in the application. In most cases, a counter variable is a primitive integer type because counter variables only track whole numbers and don’t require the extra features of an object variable type.

  • enumeration: A listing of like items that you can use to make your code easier to read. An enumeration makes it possible to refer to something using a human-readable term, even though the actual value is a number.

  • iteration: A single loop from a series of repetitive loops. A single iteration is one pass through the code in a repetitive loop structure.

  • nesting: Placing one structure within another structure of the same type. For example, nesting for loops places a subordinate for loop within a main for loop.

  • repetitive loop: A method of telling your application to perform a task more than once. The repetitive loop consists of a structure that defines where the loop begins and ends, some sort of condition used to determine how long to loop, and the series of tasks contained within the structure.

  • structure: A programming construct that begins with a statement that specifies the purpose of the structure. Normally curly braces ({}) appear after the statement to define the statement body. One or more lines of code appear within the structure body to define the tasks performed by the structure.