+6

ANY OTHER WAYS FOR CREATING INFINITE LOOP USING JAVA?

class Infinity { public static void main(String args[]) { int a=1; for(;;) { System.out.println(a); } } } OTHER THAN THIS?

10/18/2019 1:48:14 PM

NISHANT V H

35 Answers

New Answer

+21

while(true) { // do whatever }; do { // do whatever } while(true);

+9

String[] focus = { "Follow", "One", "Course", "Until", "Success" }; int i=0; int len=focus.length; while(i<len); { System.out.println(focus[i++]); } // 🍻

+6

yes its basicly different. a state of method call are saved in stack memory, so doing recrusion could fill up the stack. once its full Stackoverflow error will occur. so theoretically, its possible using recrusion as infinite loop. but due the limitation of the stack memory, its not possible.

+6

Yes, it's reserved as a keyword Ace. Gosling might have thought it was necessary at the beginning, but it was never implemented :)

+5

What Seb TheS said, infinite recursion simulates an infinite loop but isn't exactly the same. And it is possible in Java. while (true){ //code } for(int i = 1;i!=0;i++){ //code } do{ //code }while(true);

+5

Phanwadee Suriyaphen I don't believe you need the semi colon at the end of the while loop.

+4

Tashi N Yet it's recognized as a keyword. Any idea why that is? 🤔

+4

Fair enough, learning new stuff every day 😁

+4

Seems like readability was the reason to remove goto: https://stackoverflow.com/a/4547764 Comprehensible. (The true reason might be laziness tho Kartikey Sahu :D )

+3

No dude not for Hacking....Just for my knowledge.

+3

If your condition was wrong ❌ the program is infinte loop Ex: in python A=5 While(A>1) : A+=1 Print(A)

+3

You can do this for(int i=1;i++;i>0) { System.out.println("print anything u like"); }

+3

Kartikey Sahu You can't use goto in Java.

+2

Atleast recursion, (function calling itself). If you could make the program to run itself again, using command prompt or similar it could also make infinite loop, but I don't know whether it's possible with Java, propably yes.

+2

+2

Do you ask this question for hacking purposes?

+2

for(;true;) { System.out.println("hii"); }

+2

Infinite loops are usually very bad coding style... You have to avoid them...

+2

for(int i=0;i<=0;i++){ System.out.println(i); i--; }

+2

https://youtu.be/X4E0Lj5KQhs