Regarding infinite loop, need help to understand
up vote
-2
down vote
favorite
I first searched Google for my question but did not find the answer matching it.
My question is:
for(; ;){
//some code here
}
The loop is infinite, we give a condition to match, till that loop goes on.
But when we make in infinite to do the thing, again and again, so what is the limitation of the loop? I mean where does the loop stop?
Does it crash?
Or it starts again from the place it begins?
I hope I explain my question well and easy to be understandable.
java loops infinite-loop
add a comment |
up vote
-2
down vote
favorite
I first searched Google for my question but did not find the answer matching it.
My question is:
for(; ;){
//some code here
}
The loop is infinite, we give a condition to match, till that loop goes on.
But when we make in infinite to do the thing, again and again, so what is the limitation of the loop? I mean where does the loop stop?
Does it crash?
Or it starts again from the place it begins?
I hope I explain my question well and easy to be understandable.
java loops infinite-loop
@afaq7, add this code example in your question ... that will make the question more clear
– mettleap
yesterday
@afaq7 E.g.for(int i = 0; true;i++){ //sth done }
The compllator checkes every time if the condition given in the loop is true. If you puttrue
there, it would go for ever (until it runs out of memory).
– Embid123
yesterday
4
idownvotedbecau.se/noresearch, e.g. web search forinfinite loop
leading to Wikipedia saying "An infinite loop (or endless loop) is a sequence of instructions in a computer program which loops endlessly", and showing example for infinite loop, running "forever". Which part of "infinite" / "endlessly" / "forever" don't you understand? "where does loop stop?" Nowhere. It never stops.
– Andreas
yesterday
Lot of intelligent and knowledgable people have already posted here. You can read this: quora.com/…
– Pritam Banerjee
yesterday
1
"searched Google .. but did not find the answer" Link to the three closest to answering the questions. Tell us why they failed to answer the questions you have. This would demonstrate at least, a level of research, and would certainly help others to understand better how & what to explain.
– Andrew Thompson
yesterday
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I first searched Google for my question but did not find the answer matching it.
My question is:
for(; ;){
//some code here
}
The loop is infinite, we give a condition to match, till that loop goes on.
But when we make in infinite to do the thing, again and again, so what is the limitation of the loop? I mean where does the loop stop?
Does it crash?
Or it starts again from the place it begins?
I hope I explain my question well and easy to be understandable.
java loops infinite-loop
I first searched Google for my question but did not find the answer matching it.
My question is:
for(; ;){
//some code here
}
The loop is infinite, we give a condition to match, till that loop goes on.
But when we make in infinite to do the thing, again and again, so what is the limitation of the loop? I mean where does the loop stop?
Does it crash?
Or it starts again from the place it begins?
I hope I explain my question well and easy to be understandable.
java loops infinite-loop
java loops infinite-loop
edited yesterday
Andrew Thompson
152k27159333
152k27159333
asked yesterday
afaq7
126
126
@afaq7, add this code example in your question ... that will make the question more clear
– mettleap
yesterday
@afaq7 E.g.for(int i = 0; true;i++){ //sth done }
The compllator checkes every time if the condition given in the loop is true. If you puttrue
there, it would go for ever (until it runs out of memory).
– Embid123
yesterday
4
idownvotedbecau.se/noresearch, e.g. web search forinfinite loop
leading to Wikipedia saying "An infinite loop (or endless loop) is a sequence of instructions in a computer program which loops endlessly", and showing example for infinite loop, running "forever". Which part of "infinite" / "endlessly" / "forever" don't you understand? "where does loop stop?" Nowhere. It never stops.
– Andreas
yesterday
Lot of intelligent and knowledgable people have already posted here. You can read this: quora.com/…
– Pritam Banerjee
yesterday
1
"searched Google .. but did not find the answer" Link to the three closest to answering the questions. Tell us why they failed to answer the questions you have. This would demonstrate at least, a level of research, and would certainly help others to understand better how & what to explain.
– Andrew Thompson
yesterday
add a comment |
@afaq7, add this code example in your question ... that will make the question more clear
– mettleap
yesterday
@afaq7 E.g.for(int i = 0; true;i++){ //sth done }
The compllator checkes every time if the condition given in the loop is true. If you puttrue
there, it would go for ever (until it runs out of memory).
– Embid123
yesterday
4
idownvotedbecau.se/noresearch, e.g. web search forinfinite loop
leading to Wikipedia saying "An infinite loop (or endless loop) is a sequence of instructions in a computer program which loops endlessly", and showing example for infinite loop, running "forever". Which part of "infinite" / "endlessly" / "forever" don't you understand? "where does loop stop?" Nowhere. It never stops.
– Andreas
yesterday
Lot of intelligent and knowledgable people have already posted here. You can read this: quora.com/…
– Pritam Banerjee
yesterday
1
"searched Google .. but did not find the answer" Link to the three closest to answering the questions. Tell us why they failed to answer the questions you have. This would demonstrate at least, a level of research, and would certainly help others to understand better how & what to explain.
– Andrew Thompson
yesterday
@afaq7, add this code example in your question ... that will make the question more clear
– mettleap
yesterday
@afaq7, add this code example in your question ... that will make the question more clear
– mettleap
yesterday
@afaq7 E.g.
for(int i = 0; true;i++){ //sth done }
The compllator checkes every time if the condition given in the loop is true. If you put true
there, it would go for ever (until it runs out of memory).– Embid123
yesterday
@afaq7 E.g.
for(int i = 0; true;i++){ //sth done }
The compllator checkes every time if the condition given in the loop is true. If you put true
there, it would go for ever (until it runs out of memory).– Embid123
yesterday
4
4
idownvotedbecau.se/noresearch, e.g. web search for
infinite loop
leading to Wikipedia saying "An infinite loop (or endless loop) is a sequence of instructions in a computer program which loops endlessly", and showing example for infinite loop, running "forever". Which part of "infinite" / "endlessly" / "forever" don't you understand? "where does loop stop?" Nowhere. It never stops.– Andreas
yesterday
idownvotedbecau.se/noresearch, e.g. web search for
infinite loop
leading to Wikipedia saying "An infinite loop (or endless loop) is a sequence of instructions in a computer program which loops endlessly", and showing example for infinite loop, running "forever". Which part of "infinite" / "endlessly" / "forever" don't you understand? "where does loop stop?" Nowhere. It never stops.– Andreas
yesterday
Lot of intelligent and knowledgable people have already posted here. You can read this: quora.com/…
– Pritam Banerjee
yesterday
Lot of intelligent and knowledgable people have already posted here. You can read this: quora.com/…
– Pritam Banerjee
yesterday
1
1
"searched Google .. but did not find the answer" Link to the three closest to answering the questions. Tell us why they failed to answer the questions you have. This would demonstrate at least, a level of research, and would certainly help others to understand better how & what to explain.
– Andrew Thompson
yesterday
"searched Google .. but did not find the answer" Link to the three closest to answering the questions. Tell us why they failed to answer the questions you have. This would demonstrate at least, a level of research, and would certainly help others to understand better how & what to explain.
– Andrew Thompson
yesterday
add a comment |
5 Answers
5
active
oldest
votes
up vote
1
down vote
If I understood correctly, you mean to make a sort of:
while(true){/*here be code*/}
then unless you don't tell it to stop, it won't. Unless you are doing something wrong inside the loop it will not crash, it will simply do the things you specify again and again until you tell it to stop. you can do this by either placing a break;
, something stops your program or making the while's parameter a variable that can be changed inside the code eg:
int i=0;
while(i != 1){
if(?){
i++;
}
?
}
But yea, otherwise the loop will keep well, looping.
New contributor
add a comment |
up vote
0
down vote
Consider the following infinite loop in which the condition is omitted:
for(int i=1;;i++){
System.out.println(i);
}
Yes, the loop will continue unless interrupted (ctrl+ c) or your laptop's battery die out!
Output:
1
2
3
.
.
Running the loop again will begin for the initialization i.e. 1
You may want to read up the concepts of memory (Stack overflow!) to understand how it will not go on forever in practice.
add a comment |
up vote
0
down vote
An infinity loop will keep on going until you stop it, or your computer dies.
You need to make sure your loop stops at somepoint, like when you reach the end of an arraylist.
New contributor
add a comment |
up vote
-1
down vote
Do you mean when the loop is infinite?
There's many ways to make an infinite loop. But if you realy want to do so I guess by convention it's not recommended because the program will run until it run out of memory.
Let's take as exemple the code bellow that will print how many times you want to show something happening but you forgot to put an incrementer so you made an infinite loop that will run over and over until your machine could not handle anymore.
int count = 0;
while(count <= 42) {
System.out.print(count + " Times running your program");
}
I'm sorry if you got you wrong.
Your example should never "run out of memory" ... because nothing in the loop ever allocates more memory. The laptop it's executing on might run out of battery. But it shouldn't run out of memory. ALSO: One thing that nobody's mentioned is that an "infinite loop" ISN'T NECESSARILY BAD. One example is an application's "main event loop": something at the heart of most every Windows, Mac and Linux GUI program. It loops forever - processing UI events. Until it receives an "Exit" event. Which instructs the app to terminate itself (often with an "exit()" system call)..
– paulsm4
yesterday
add a comment |
up vote
-2
down vote
Below is a simple infinite loop :
while(true) ;
The above loop does nothing, it will just keep going on.
In the loop we are not using any variable or object so memory consumption is bare minimum.
I think it will go on until somehow JVM gets terminated intentionally/unintentionally or it runs out of memory/memory related error or when the main thread gets terminated.
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
If I understood correctly, you mean to make a sort of:
while(true){/*here be code*/}
then unless you don't tell it to stop, it won't. Unless you are doing something wrong inside the loop it will not crash, it will simply do the things you specify again and again until you tell it to stop. you can do this by either placing a break;
, something stops your program or making the while's parameter a variable that can be changed inside the code eg:
int i=0;
while(i != 1){
if(?){
i++;
}
?
}
But yea, otherwise the loop will keep well, looping.
New contributor
add a comment |
up vote
1
down vote
If I understood correctly, you mean to make a sort of:
while(true){/*here be code*/}
then unless you don't tell it to stop, it won't. Unless you are doing something wrong inside the loop it will not crash, it will simply do the things you specify again and again until you tell it to stop. you can do this by either placing a break;
, something stops your program or making the while's parameter a variable that can be changed inside the code eg:
int i=0;
while(i != 1){
if(?){
i++;
}
?
}
But yea, otherwise the loop will keep well, looping.
New contributor
add a comment |
up vote
1
down vote
up vote
1
down vote
If I understood correctly, you mean to make a sort of:
while(true){/*here be code*/}
then unless you don't tell it to stop, it won't. Unless you are doing something wrong inside the loop it will not crash, it will simply do the things you specify again and again until you tell it to stop. you can do this by either placing a break;
, something stops your program or making the while's parameter a variable that can be changed inside the code eg:
int i=0;
while(i != 1){
if(?){
i++;
}
?
}
But yea, otherwise the loop will keep well, looping.
New contributor
If I understood correctly, you mean to make a sort of:
while(true){/*here be code*/}
then unless you don't tell it to stop, it won't. Unless you are doing something wrong inside the loop it will not crash, it will simply do the things you specify again and again until you tell it to stop. you can do this by either placing a break;
, something stops your program or making the while's parameter a variable that can be changed inside the code eg:
int i=0;
while(i != 1){
if(?){
i++;
}
?
}
But yea, otherwise the loop will keep well, looping.
New contributor
New contributor
answered yesterday
Alejo Niss
113
113
New contributor
New contributor
add a comment |
add a comment |
up vote
0
down vote
Consider the following infinite loop in which the condition is omitted:
for(int i=1;;i++){
System.out.println(i);
}
Yes, the loop will continue unless interrupted (ctrl+ c) or your laptop's battery die out!
Output:
1
2
3
.
.
Running the loop again will begin for the initialization i.e. 1
You may want to read up the concepts of memory (Stack overflow!) to understand how it will not go on forever in practice.
add a comment |
up vote
0
down vote
Consider the following infinite loop in which the condition is omitted:
for(int i=1;;i++){
System.out.println(i);
}
Yes, the loop will continue unless interrupted (ctrl+ c) or your laptop's battery die out!
Output:
1
2
3
.
.
Running the loop again will begin for the initialization i.e. 1
You may want to read up the concepts of memory (Stack overflow!) to understand how it will not go on forever in practice.
add a comment |
up vote
0
down vote
up vote
0
down vote
Consider the following infinite loop in which the condition is omitted:
for(int i=1;;i++){
System.out.println(i);
}
Yes, the loop will continue unless interrupted (ctrl+ c) or your laptop's battery die out!
Output:
1
2
3
.
.
Running the loop again will begin for the initialization i.e. 1
You may want to read up the concepts of memory (Stack overflow!) to understand how it will not go on forever in practice.
Consider the following infinite loop in which the condition is omitted:
for(int i=1;;i++){
System.out.println(i);
}
Yes, the loop will continue unless interrupted (ctrl+ c) or your laptop's battery die out!
Output:
1
2
3
.
.
Running the loop again will begin for the initialization i.e. 1
You may want to read up the concepts of memory (Stack overflow!) to understand how it will not go on forever in practice.
answered yesterday
Chica_Programmador
23110
23110
add a comment |
add a comment |
up vote
0
down vote
An infinity loop will keep on going until you stop it, or your computer dies.
You need to make sure your loop stops at somepoint, like when you reach the end of an arraylist.
New contributor
add a comment |
up vote
0
down vote
An infinity loop will keep on going until you stop it, or your computer dies.
You need to make sure your loop stops at somepoint, like when you reach the end of an arraylist.
New contributor
add a comment |
up vote
0
down vote
up vote
0
down vote
An infinity loop will keep on going until you stop it, or your computer dies.
You need to make sure your loop stops at somepoint, like when you reach the end of an arraylist.
New contributor
An infinity loop will keep on going until you stop it, or your computer dies.
You need to make sure your loop stops at somepoint, like when you reach the end of an arraylist.
New contributor
New contributor
answered yesterday
Skou
12
12
New contributor
New contributor
add a comment |
add a comment |
up vote
-1
down vote
Do you mean when the loop is infinite?
There's many ways to make an infinite loop. But if you realy want to do so I guess by convention it's not recommended because the program will run until it run out of memory.
Let's take as exemple the code bellow that will print how many times you want to show something happening but you forgot to put an incrementer so you made an infinite loop that will run over and over until your machine could not handle anymore.
int count = 0;
while(count <= 42) {
System.out.print(count + " Times running your program");
}
I'm sorry if you got you wrong.
Your example should never "run out of memory" ... because nothing in the loop ever allocates more memory. The laptop it's executing on might run out of battery. But it shouldn't run out of memory. ALSO: One thing that nobody's mentioned is that an "infinite loop" ISN'T NECESSARILY BAD. One example is an application's "main event loop": something at the heart of most every Windows, Mac and Linux GUI program. It loops forever - processing UI events. Until it receives an "Exit" event. Which instructs the app to terminate itself (often with an "exit()" system call)..
– paulsm4
yesterday
add a comment |
up vote
-1
down vote
Do you mean when the loop is infinite?
There's many ways to make an infinite loop. But if you realy want to do so I guess by convention it's not recommended because the program will run until it run out of memory.
Let's take as exemple the code bellow that will print how many times you want to show something happening but you forgot to put an incrementer so you made an infinite loop that will run over and over until your machine could not handle anymore.
int count = 0;
while(count <= 42) {
System.out.print(count + " Times running your program");
}
I'm sorry if you got you wrong.
Your example should never "run out of memory" ... because nothing in the loop ever allocates more memory. The laptop it's executing on might run out of battery. But it shouldn't run out of memory. ALSO: One thing that nobody's mentioned is that an "infinite loop" ISN'T NECESSARILY BAD. One example is an application's "main event loop": something at the heart of most every Windows, Mac and Linux GUI program. It loops forever - processing UI events. Until it receives an "Exit" event. Which instructs the app to terminate itself (often with an "exit()" system call)..
– paulsm4
yesterday
add a comment |
up vote
-1
down vote
up vote
-1
down vote
Do you mean when the loop is infinite?
There's many ways to make an infinite loop. But if you realy want to do so I guess by convention it's not recommended because the program will run until it run out of memory.
Let's take as exemple the code bellow that will print how many times you want to show something happening but you forgot to put an incrementer so you made an infinite loop that will run over and over until your machine could not handle anymore.
int count = 0;
while(count <= 42) {
System.out.print(count + " Times running your program");
}
I'm sorry if you got you wrong.
Do you mean when the loop is infinite?
There's many ways to make an infinite loop. But if you realy want to do so I guess by convention it's not recommended because the program will run until it run out of memory.
Let's take as exemple the code bellow that will print how many times you want to show something happening but you forgot to put an incrementer so you made an infinite loop that will run over and over until your machine could not handle anymore.
int count = 0;
while(count <= 42) {
System.out.print(count + " Times running your program");
}
I'm sorry if you got you wrong.
answered yesterday
TBrito1
13
13
Your example should never "run out of memory" ... because nothing in the loop ever allocates more memory. The laptop it's executing on might run out of battery. But it shouldn't run out of memory. ALSO: One thing that nobody's mentioned is that an "infinite loop" ISN'T NECESSARILY BAD. One example is an application's "main event loop": something at the heart of most every Windows, Mac and Linux GUI program. It loops forever - processing UI events. Until it receives an "Exit" event. Which instructs the app to terminate itself (often with an "exit()" system call)..
– paulsm4
yesterday
add a comment |
Your example should never "run out of memory" ... because nothing in the loop ever allocates more memory. The laptop it's executing on might run out of battery. But it shouldn't run out of memory. ALSO: One thing that nobody's mentioned is that an "infinite loop" ISN'T NECESSARILY BAD. One example is an application's "main event loop": something at the heart of most every Windows, Mac and Linux GUI program. It loops forever - processing UI events. Until it receives an "Exit" event. Which instructs the app to terminate itself (often with an "exit()" system call)..
– paulsm4
yesterday
Your example should never "run out of memory" ... because nothing in the loop ever allocates more memory. The laptop it's executing on might run out of battery. But it shouldn't run out of memory. ALSO: One thing that nobody's mentioned is that an "infinite loop" ISN'T NECESSARILY BAD. One example is an application's "main event loop": something at the heart of most every Windows, Mac and Linux GUI program. It loops forever - processing UI events. Until it receives an "Exit" event. Which instructs the app to terminate itself (often with an "exit()" system call)..
– paulsm4
yesterday
Your example should never "run out of memory" ... because nothing in the loop ever allocates more memory. The laptop it's executing on might run out of battery. But it shouldn't run out of memory. ALSO: One thing that nobody's mentioned is that an "infinite loop" ISN'T NECESSARILY BAD. One example is an application's "main event loop": something at the heart of most every Windows, Mac and Linux GUI program. It loops forever - processing UI events. Until it receives an "Exit" event. Which instructs the app to terminate itself (often with an "exit()" system call)..
– paulsm4
yesterday
add a comment |
up vote
-2
down vote
Below is a simple infinite loop :
while(true) ;
The above loop does nothing, it will just keep going on.
In the loop we are not using any variable or object so memory consumption is bare minimum.
I think it will go on until somehow JVM gets terminated intentionally/unintentionally or it runs out of memory/memory related error or when the main thread gets terminated.
add a comment |
up vote
-2
down vote
Below is a simple infinite loop :
while(true) ;
The above loop does nothing, it will just keep going on.
In the loop we are not using any variable or object so memory consumption is bare minimum.
I think it will go on until somehow JVM gets terminated intentionally/unintentionally or it runs out of memory/memory related error or when the main thread gets terminated.
add a comment |
up vote
-2
down vote
up vote
-2
down vote
Below is a simple infinite loop :
while(true) ;
The above loop does nothing, it will just keep going on.
In the loop we are not using any variable or object so memory consumption is bare minimum.
I think it will go on until somehow JVM gets terminated intentionally/unintentionally or it runs out of memory/memory related error or when the main thread gets terminated.
Below is a simple infinite loop :
while(true) ;
The above loop does nothing, it will just keep going on.
In the loop we are not using any variable or object so memory consumption is bare minimum.
I think it will go on until somehow JVM gets terminated intentionally/unintentionally or it runs out of memory/memory related error or when the main thread gets terminated.
answered yesterday
Nitesh Patil
12
12
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343499%2fregarding-infinite-loop-need-help-to-understand%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
@afaq7, add this code example in your question ... that will make the question more clear
– mettleap
yesterday
@afaq7 E.g.
for(int i = 0; true;i++){ //sth done }
The compllator checkes every time if the condition given in the loop is true. If you puttrue
there, it would go for ever (until it runs out of memory).– Embid123
yesterday
4
idownvotedbecau.se/noresearch, e.g. web search for
infinite loop
leading to Wikipedia saying "An infinite loop (or endless loop) is a sequence of instructions in a computer program which loops endlessly", and showing example for infinite loop, running "forever". Which part of "infinite" / "endlessly" / "forever" don't you understand? "where does loop stop?" Nowhere. It never stops.– Andreas
yesterday
Lot of intelligent and knowledgable people have already posted here. You can read this: quora.com/…
– Pritam Banerjee
yesterday
1
"searched Google .. but did not find the answer" Link to the three closest to answering the questions. Tell us why they failed to answer the questions you have. This would demonstrate at least, a level of research, and would certainly help others to understand better how & what to explain.
– Andrew Thompson
yesterday