Infinite for (Java) loop

In Java, as in almost any programming language, there are tools to ensure the repeated repetition of a certain piece of code, or, as they are called, loops. Java loops are represented by operators such as for and while, as well as their variations. As a rule, loops are used to go through one-dimensional and multi-dimensional arrays and iterable data structures (including collections) in order to find certain elements and further operations with them. However, this is not the only way to use a tool like the Java loop. Use cases will be provided as they are reviewed.

Java while loop : description and examples

The underlying loop operator in Java is while. A code fragment enclosed in its body will be repeated until the condition of the expression enclosed in brackets after it satisfies the logical value of truth. The general form of the while statement is as follows:

while (condition) {

// loop body

}

As soon as the value of the logical condition ceases to be true, the code enclosed in the body of the loop will cease to be executed and control will be transferred to the line immediately after it.

loops in java




If only one operator is enclosed in the loop body, then curly brackets can be omitted, but it is considered good form if they always stand. The figure above shows a block diagram of the operation of this operator.





For clarity, let's analyze the example presented in the figure below:

while java loop




count 1. , . , .. true, , count 10. () 1 . , 11, .

count 11, , .

, Java while . . , i = 100 j = 200, – «» while:

while(++i < --j);

. , .









do-while

false, . , , , . , , . while do-while. :

do {

//

} while();

, , – .

java loop examples




, , , while. count 11, , .

for – Java

for Java. Java SDK for, – for each. . for Java :

for java loop




, i, . , , true, . , , . - .

for java loop




, for Java :

for java loop




, loopVal. 1, , 11. , for, , . , .

, . For Java , , . :

for(int i = 1, int j = 10; i < j; ++i, --j) {}

i 1, j 1. , i j.

for

for , (, /) . , .

boolean exit = false;

for (int i = 0; !exit; ++i) {

exit = true;

}

, i , exit true. : for( ; !exit; ) {}. , . - , , .

For Java : for( ; ; ) {}. . , , .

for each

foreach Java - . for :

foreach java loop




name, names . name , . , , . name .

: break, return continue. , continue . Java, . break:

exit java loop




for 11 , 8, , i 7, , break.

return , , Java, , .

break goto

, break , , .. , . break goto.

, , . . . , : break _. :

java infinite loop




One, Two Three . break Two , Three Two One. .. : Three One.

Java, while for, do-while for each . , .




All Articles