This question already has answers here:
How does a for loop work, specifically for(;;)?
(6 answers)
Closed 7 years ago.
There are a lots of option available for infinite loop but this are mostly use while(true) or for(;;)
I know while(true) is the best option, since it is easier to understand.
But I want to use for(;;).
I want to know what is going on inside of for loop when I we used two ; within for loop.
for(;;)
Semicolon means its an empty statement. But how its works when we use inside of for loop for infinite execution?
This is what happens:
for (initialization statement; condition check; increment/decrement)
// loop body;
With for(;;):
There is no initialization.
There is no condition check.
There is no increment/decrement.
Therefore it will run forever, exactly like while(true).
Related
This question already has answers here:
How does the Java 'for each' loop work?
(29 answers)
Closed 5 years ago.
How this for loop statement works? Can we only use it to print arrays?
for (Integer i : arrayToSort){
System.out.println(i.intValue());
}
This is the for-each loop, you can use it like the normal for-loop. Only the syntax is different. Please look at the https://docs.oracle.com/javase/8/docs/technotes/guides/language/foreach.html
This question already has answers here:
How do I break multiple foreach loops? [duplicate]
(9 answers)
How do I break out of nested loops in Java?
(37 answers)
Closed 5 years ago.
Does the keyword 'break' only jump out of one layer of loop? In this piece of code, there are 3 layers loops, which I want to jump out completely. Is there an easy to do that?
while(){
while(){
for(){
if(a==b){
// I want to jump out of the outer while() loop.
}
}
}
}
// and the logic continues from this line.
This question already has answers here:
Java, How do I get current index/key in "for each" loop [duplicate]
(6 answers)
Closed 7 years ago.
I've been wondering if it is somehow possible to get the current index of a for each loop in Java without a seperate count variable.
In debug mode I can see the loop in the following structure:
i$ = {ArrayList$Itr#17838}
cursor = 1
lastRet = 0
expectedModCount = 5
this$0 = {ArrayList#17836}
So I guess cursor is the index. Is there a way to access the cursor?
Thanks for your help
So far
Not to my knowledge. But even if there were a way, it would be counterproductive to access. The reason why one would want a for:each loop is because it is quick, and cycles through a whole array in a simple manor. If you want to have a cursor index to access, use a for loop and just access the incrementing variable.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I didn't find any practical use of do-while loop. As a studied we don't have control over the do-while loop when it executes for the first time and everything which can be done with do-while loop, can be don with while or for loop (assuming).
My Question is: Is there any situation where we use a do-while loop, or any particular situation where it gives more reliable results as compared to for and while loops?
it's useful for repeating an action until it's correct
String input;
...
do
{
input = getInput();
} while(isValid(input));
to do that with another loop, you'd have to write input = getInput(); twice
do/while is precisely suitable when you always want to execute at least once, but you want to check whether or not to keep going at the end of the loop instead of at the start.
You can easily emulate it with a while loop, but sometimes the code becomes less clear that way. All three loop types can be emulated with each other, with more or less mess - but as all three are available, you should just pick the most appropriate loop for the job in hand.
I personally use both while loops and do/while loops less often than for loops, but sometimes a do/while is the most readable option.
Might as well say "I didn't find any practical use of for loop...everything which can be done with for loop, can be don with while loop (assuming)".
Technically you can replace any loop with literally any other loop given enough finangling with the loop guards and such. Some loops are just nicer to read than others.
do-while is primarily "better" if you want to perform an action at least once, e.g. asking for input, or getting some random value until it fits into some constraint. You can do such an action by performing the action once outside a "regular" loop and looping from there, but it's just neater to use do-while.
Do-while has generally better performance characteristics if the loop body must execute at least once. If you reformulate a do-while into a while/for, you add an extra evaluation of the loop exit condition.
Also, do-while(false) is usable as a structured alternative for deeply nested if conditions:
do {
if (!condition1)
break;
if (!condition2)
break;
if (!conditionN)
break;
// do something
} while (false);
instead of:
if (condition1) {
if (condition2) {
if (conditionN) {
// do something
}
}
}
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I have a program that I am writing and I have it completed but I keep receiving an error in a counter that I am using. The purpose of the program is to build a plate stacker and I have finished that but need to write a counter based on the particular colors used (how many green plates...etc.). As I know several classmates use this site to cheat, I will not post the code in whole but only the fragments I feel are necessary. Below is an example of a statement that I am using to attempt to get a count of the beige plates in the text file. The statement is in a while loop with the other colors having the same coding. I also have initialized the counter (int beige = 0;) prior to this and outside of the while loop:
if(line == "beige")
{
beige++;
}
I have a System.out following these statements to display the count but it always comes up as 0 so I believe the problem is in this particular fragment. If I need to send additional coding I will but I believe it is something to do with this or the placement of it in the while loop.
First, you are comparing the string in a wrong way. Use .equals method.
if (line.equals("beige"))
beige++;
Second, you can use HashMap for this kind of problem where you keep updating the value(count) for key(color) and it will save you a lot of if conditions if say there are a hundred different colors!