ANY OTHER WAYS FOR CREATING INFINITE LOOP USING JAVA? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

18th Oct 2019, 1:48 PM
Nishant Hegde
Nishant Hegde - avatar
30 Answers
+ 20
while(true) { // do whatever }; do { // do whatever } while(true);
18th Oct 2019, 1:53 PM
Tashi N
Tashi N - avatar
+ 9
String[] focus = { "Follow", "One", "Course", "Until", "Success" }; int i=0; int len=focus.length; while(i<len); { System.out.println(focus[i++]); } // 🍻
20th Oct 2019, 7:58 AM
Danijel Ivanović
Danijel Ivanović - avatar
+ 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.
18th Oct 2019, 2:15 PM
Taste
Taste - avatar
+ 6
Yes, it's reserved as a keyword Ace. Gosling might have thought it was necessary at the beginning, but it was never implemented :)
20th Oct 2019, 6:30 AM
Tashi N
Tashi N - avatar
+ 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);
18th Oct 2019, 2:00 PM
Odyel
Odyel - avatar
+ 5
Phanwadee Suriyaphen I don't believe you need the semi colon at the end of the while loop.
21st Oct 2019, 12:53 AM
Sonic
Sonic - avatar
+ 4
Kartikey Sahu You can't use goto in Java.
20th Oct 2019, 6:10 AM
Tashi N
Tashi N - avatar
+ 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 )
20th Oct 2019, 7:10 AM
Tashi N
Tashi N - avatar
+ 3
No dude not for Hacking....Just for my knowledge.
19th Oct 2019, 3:16 AM
Nishant Hegde
Nishant Hegde - avatar
+ 3
If your condition was wrong ❌ the program is infinte loop Ex: in python A=5 While(A>1) : A+=1 Print(A)
19th Oct 2019, 3:00 PM
ant
ant - avatar
+ 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.
18th Oct 2019, 1:51 PM
Seb TheS
Seb TheS - avatar
+ 2
for(;;)
18th Oct 2019, 10:43 PM
Haider Sultan
Haider Sultan - avatar
+ 2
for(;true;) { System.out.println("hii"); }
19th Oct 2019, 1:28 PM
Lakshmi kanth N V
Lakshmi kanth N V - avatar
+ 2
Infinite loops are usually very bad coding style... You have to avoid them...
19th Oct 2019, 5:17 PM
Loïc Mahé
Loïc Mahé - avatar
+ 2
YES ask your expert
20th Oct 2019, 7:41 AM
Imran Alam
Imran Alam - avatar
+ 2
for(int i=0;i<=0;i++){ System.out.println(i); i--; }
20th Oct 2019, 9:47 AM
StackOverride
StackOverride - avatar
+ 2
Kartikey Sahu why was Gosling lazy?
20th Oct 2019, 12:05 PM
Sonic
Sonic - avatar
+ 1
In any loop(for,while,do while,), and recursive function, after start of execution, if there is no base condition or exit condition to exit, then loops infinitely. Adding to above examples: (Tashi N, kilowac said..) returntype fun(...){ -- fun(...); -- } Ex: int fun(int i){ return i*fun(i-1); }
18th Oct 2019, 3:43 PM
Jayakrishna 🇮🇳
+ 1
while(true); for(;;);
18th Oct 2019, 4:37 PM
RamonR
RamonR - avatar
+ 1
} While(){ }
18th Oct 2019, 7:47 PM
Daniel Ikokoh
Daniel Ikokoh - avatar