Is it possible to write a for loop to create an infinite loop, or is it only when this is done?

Can a for loop be written in Java to create an infinite loop, or is it only when the loops cause this problem?

+6
source share
12 answers
for(;;){} 

coincides with

 while(true){} 
+14
source

You can also do this for loops. For instance. the following corresponds to while(true) :

 for(;;) { } 
+8
source

In addition to scope issues and one more thing, these are:

 for(<init>; <test>; <step>) { <body> } 

matches with:

 <init> while(<test>) { <body> <step> } 

Like other people, just as you can have a while without a <init> or <step> form, you can have a for loop without them:

 while(<test>) { <body> } 

coincides with

 for(;<test>;) { <body> } //Although this is terrible style 

And finally, you may have

 for(;true;) { <body> } 

Now, remember when I said there is one more thing? This is because for loops do not need a test - this gives a solution that everyone else posted:

 for(;;) { <body> } 
+8
source

Of course you can

 for(int i = 0; i == i; i++) {} 

Any cycle can be infinite as long as you make a way to never hit the exit conditions.

+6
source

Do not forget mistakes, for example

 for (int i=0; i<30; i++) { //code i--; } 

This is not as unusual as it should be.

+4
source

Just for fun (and this is too long for comment): a lot of people will be very surprised to learn that for many practical purposes the following cycle is an almost endless cycle:

  for (long i = Long.MIN_VALUE; i < Long.MAX_VALUE; i++) { ... } 

If a thread executing these loops can do 4 billion cycles per second and can do incrementing and checking for one loop (quite muscular for a single thread), and if my math data is not completely disabled, I think the above code needs about 150 years to complete :)

+3
source

I will just add this since no one has made this version:

 for(;;); 
+2
source

How I started my server until I close it,

for(int i = 0;i<-1;i++){//code here}

+2
source

Of course, infinite loops can occur for loops. Example:

for (int i = 0; i <99; i / = 2) {...}

Since i never increases, it will remain in the body of the for loop forever until you exit the program.

+1
source

There is this one to complete the topic:

 do {something();} while(true); 
0
source

There are many ways to do endless loops. This is the solution I found:

 int j = 1; for (int i = 0; i < j; i++) { //insert code here j++; } 

This works very well for me, because it allows the loop to check an infinite number of values, and also infinitely cyclically.

0
source

you can declare a subsection of a piece of code another part of a for loop, for example -

 public class (classname) { for(int i = 1; i <= 4; i++) { for(int j = 1; i <= 4; j++) { system.out.println(i + "*" + j + "=" (i*j)); } 

}

it is almost endless; if you change int to long and add more variables, you can practically do this 25 x 10 ^ 12 minutes

-1
source

Source: https://habr.com/ru/post/890527/


All Articles