How to create and break Java loop

How to create loops in Java and manage them. How to automate the arrays’ processing and objects’ generation with the help of them.


A loop is a block of commands that are being executed over and over again until the loop condition is equal to true. The repeated code fragment is called the loop cycle. A single loop cycle execution is called an iteration.


You can work with several loop types in Java:


while – a cycle with a precondition – first we check the condition, then we execute the loop cycle ;


do…while – a loop with a postcondition – first we execute the loop body once, then we check the condition and if it is true, we continue;


for — a loop with a counter — runs and updates the counter with each iteration while the condition in the loop declaration is true;


short for (in other programming languages is known as foreach) – loops the array from the first element to the last and executes the loop cycle within each iteration.


The essence of the loop condition is the expression verification with one or several variables: “As long as a <11, in each iteration we execute the loop cycle and increase “a” by 1”. But what happens if “a” is equal to the first execution of the cycle?


If we use constructions with while, the value must be specified before the start of the cycle:


int a = 1;

while (a < 11) {


a++; //increment a for 1



If the variable works as the loop counter and is not used outside this loop, then it should be initialised directly in the condition. And here you specify what should be done with it at the end of each iteration. Here is the one-line for loop condition:


for (а=1, a<11, i++) {




We get the same result. The list could be started from scratch or from a negative value – we define the range ourselves.


The abbreviated for loop does not indicate the number of repetitions, nor the actions at the end of the step. A foreach loop is used to iterate through arrays. From the first element, you need to go to the next – until the array is over.


int[] ms = { 1, 2, 3, 4}; //creating an array

int s = 0;

for(int i : ms) { //here we specify what to loop

s *= i; //consistently multiplying the elements




Java nested loops

The loops can be nested. In such a situation, the repeat count of external and nest loops is multiplied. In case the external and nested loops should be executed 5 times, then the cycle will be executed 25 times generally.


let’s output the multiplication table with the help of two arrays:


int a, b, result = 0;

for (a = 2; a < 10; a++) {

for (b = 2; b < 10; b++) {

    result = a*b;

    System.out.println(a+"x"+b+" = "+result);




Creating objects in Java loops

Loops are handy when you need to create and number many objects. Their number is unknown in advance: objects can be created upon user request. So we asked how much something was needed and wrote the number in the variable n. Now create objects in the right quantity:


Something[] array = new Something[n]; //creating an array like something from n elements

for(int i = 0; i < n; i++){

  array[i] = new Something(); //creating something and putting it into the loop


How to exit Java loop

In order to exit a loop, you can use such keywords as break, continue and return. Break switches the application to execute the following operators after the loop. Conditions of breaking the loop in Java are executed through if branching. The main thing is to execute the validation before the main part of the loop cycle.


//after creating an array m, we write the following:


for (a : m) {

   if (a==5) break;




Java branch and loop operators often work together: we start a cycle, and inside we check whether the condition for which we need to break the cycle or do something else is not executed.


If you use break operator in a nested loop, only it will break, and the router will continue to run.


In order to preliminary break the Java loop iteration, we use a continue operator. When the application code reaches it, it skips the incomplete part of the iteration, updates the counter, and proceeds to the next iteration.


In the while constructions, the same continue works differently: it returns us to check the conditions of the continuation of the loop. Another command – return – returns the program to the place from which the method in which the loop is located was called.


Both continue and break operators can be used with a cue point which allows turning to a particular code part, which is an analogue to goto:


break mark1; // provided that Mark1 is somewhere higher;


Java infinite loop

It is pretty easy to create an infinite loop – just skip the parameters in the loop:


for (;;) {}


It is much more difficult to benefit from it. Usually, the infinite cycle is a critical error that interferes with application execution. Therefore, each cycle should be checked for the ability to correctly complete at the right time. For this you need:

  • specify interrupt conditions in the loop body,

  • make sure that the variable in the interrupt condition can take the value at which the cycle will be stopped.

Leave a Comment