Is there a way to pause a for loop in java? So is there a way to only go forward one iteration when prompted? I read this http://answers.yahoo.com/question/index?qid=20100212201605AAazS73 and the solution seems like it could have some problems mostly because I don't fully understand the order the for loop checks its header. The only method I could think of that could accomplish something similar is the following
do {
if (FLAG) {
//Do procedure
i++;
FLAG = false;
}
} while ( i < 6);
When the flag is true the procedure is done and the counter moves forward one. I don't like this, though, because it will keep looping as long as the counter is below 6, if I am not mistaken. Any ideas?
-Sorry for the lack of clarity. The FLAG in my case would be a static boolean that could be called from another class. The procedure I allude to is dependent on i.
When iterating through a for loop, for example, the one below, it does the following
for (int i = 0; i < 6; i++) {
// Do stuff
}
It declares the variable i and assigns a value of 0.
It checks the conditional i < 6. If true, then proceed to step 3. Otherwise go to step 6.
Goes through the body of the loop.
Increment the variable i due to the i++ in the for loop header.
Go to step 2.
The loop ends.
As for your objective, I'm not sure what your objective is. Are you looking to pause using a blocking method call? If so, then something like this would work:
for (int i = 0; i < 6; i++) {
System.in.readLine();
}
Alternatively, you could use some sort of flag that polls to check whether the loop should proceed, such as:
for (int i = 0; i < 6; i++) {
while (paused) {
// An infinite loop that keeps on going until the pause flag is set to false
}
}
Hope this helped.
It's not clear what sort of "prompt" you mean. You could certainly do something like:
for (int i = 0; i < 6; i++) {
System.out.println("Press return to continue...");
System.in.readLine();
// Do the body of the loop
}
That's appropriate for a console app, but obviously not for (say) a Swing app. It also doesn't address the FLAG part of your sample code, because it's not clear what that's meant to mean. Are you trying to prompt the user for more information, or just confirmation to continue? If you could clarify what you're trying to achieve, that would really help.
For the sake of testability, if this is for non-throwaway code you may want to extract the idea of a user prompt, so you can test with an implementation which doesn't actually prompt the user, but just records that it would have done so.
This would involve blocking the Thread that the for loop occupies.
You can do this simply, but not very well, with this:
for(something)
while(!FLAG)
//Do procedure
Another way would be to have another Thread going on, and have the main thread wait for that other thread.
Here's some more information: How to make a Java thread wait for another thread's output?
Your goal is somewhat unclear. I think you want your program to keep running until you get six of a certain input and if so, that approach will work, though of course you'll need to get input from the user to move the loop forward.
If your concern is that the while loop would use a lot of system resources, that will not be a problem.
Well, you could use Thread.Sleep(); to pause for a little bit between flag checks, but what you are really looking for is a function that blocks while waiting for input, System.in.readline(); blocks if I recall correctly ;) Like so:
int i = 0
do
{
if(FLAG)
{
//Do stuff
i++;
//Clear Flag
}
Thread.Sleep(50); //Sleep for 50 ms
} while (i < 6);
or like so:
for(int i = 0; i < 6; i++) //Execute readline 6 times.
{
System.in.readLine();
}
Others have covered how to use System.in.readLine() to have the program explicitly require action from the user.
If what you need is not to have the program wait for the user, but allow you - the programmer - to slow the program down, in order to be able to find and fix a bug, you might want to consider using a debugger as this is exactly what it is designed for.
Any modern Java IDE has a debugger. The keys chosen to use it, just varies.
If you are using Eclipse, you are using either F11 or Ctrl-F11 (assuming Windows) to run your program. The difference is that F11 starts your program inside a debugger and Ctrl-F11 doesn't.
Put the cursor at the first line inside the for-loop, and choose Run->Toggle Breakpoint. A blue bullet will show to the left of the line. This indicates that the breakpoint is active - the debugger will now stop your program every time it reaches that line.
Now run your program in the debugger with F11. The program stops at the line, and you can investigate your variables in the Variables pane as needed, and continue execution with F8 whenever you are ready.
Related
The delay1 can only contain agent.paint. On the exit of the delay1, a new agent gets released from the delay.
But the released agent is not an agent.paint instead it is an agent.repair or agent.bwork.
The delay2, delay4, and delay7 can contain agent.repair and agent.bwork but these delays are full, so now the agent is a problem, which is not supposed to happen.
Problem:
In delay1 only agent.paint can go, after getting free from delay and In delay2, delay4 or delay7 only agent.repair or agent.bwork can go, if delay have them.
And that is not happening in the existing model.
I try to explain in the image below:
My existing approach which is no working.
There are several things that are incorrect in your model. First of all, please keep in mind that when you use the word agent in a flowchart block, it always refers to the agent contained in the current flowchart block from where you are calling it.
I assume, in the "on exit" function of delay1 you want to release an agent from delay which has a parameter paint == true. However, you wrote the function as
if (delay.size() > 0 && agent.paint == true) delay.stopDelay(delay.get(0));
The second condition agent.paint == true refers to the current agent in delay1 flowchart block and not the agent in the delay that you want to release. Additionally, you are calling stopDelay() for the agent at position 0 in the delay block, regardless of whether this agent is the correct agent.
Instead, you need to iterate over all agents contained in the delay flowchart block, find the agent that meets your condition and release this specific agent. Your code can look like this:
if (delay.size() > 0) {
Agent agentToRelease = null;
for (int i = 0; i++; i < delay.size() {
if (delay.get(i).paint == true) { // Note: The function delay.get(i) is very slow. See if you can use Collection instead.
agentToRelease = delay.get(i);
break;
}
}
if (agentToRelease != null) {
delay.stopDelay(agentToRelease);
}
}
The same goes for delay2, delay4 and delay7.
The "on enter" function of delay is always called by the agent that is currently entering the delay. Because you check the condition for this specific agent here, you can directly call stopDelay() for this agent in case the condition returns true. So the first part of your code should look like this (the same goes for the remaining conditions)
if (agent.paint == true && delay1.size() + moveTo.size() < 2) {
delay.stopDelay(agent);
} else if (...)
in delay you are using stopDelay with an agent without caring for the value of its 3 parameters.
So it's perfectly possible to release an agent that is NOT paint when delay1 is empty... and this agent will go to any of the other 3 delays... no reason why not to.
Besides this.. you have moveTo blocks that mess up your condition... so if an agent is in moveTo4 and delay2 is empty, it will go to delay 2 anyways... because why not? You don't have any restriction for that to happen
You are overcomplicating an extremely simple problem
So I have this code and it is inside a while(MainMenu) and what i want to learn is how to restart my code.. Sometimes using MainMenu=false; and then MainMenu=true; works but sometimes it doesnt causing me to use return to crash the program because the user can cheat...
if(CitizenLoveNum<=20){
JOptionPane.showMessageDialog(null, "Your citizen are protesting against you! You need to stop them now before something really bad happens", "Citizens on Strike!", JOptionPane.WARNING_MESSAGE);
String Strike=JOptionPane.showInputDialog("Choose 1 of the options below! \n 1.Pay Them (-1000$) \n 2.Send Army (With this move you can lose from 0 to 5 men!)", "Citizens on Strike!");
if(Strike.equals("1")){
if(MoneyCount<=0){
JOptionPane.showMessageDialog(null, "Out Of Money! Try again later..", "No Money Left", JOptionPane.WARNING_MESSAGE);
return;
}
JOptionPane.showMessageDialog(null,"The Citizen's are calm once more. But try buying them recources so that won't happen again!", "Citizens on Strike!",JOptionPane.INFORMATION_MESSAGE);
MoneyCount-=1000;
MainMenu=false;
MainMenu=true;
}else{
Random Army= new Random();
int ArmyNum = Army.nextInt(5 - 0) + 0;
JOptionPane.showMessageDialog(null,"You have sent the army. You lost "+ArmyNum+" Soldiers.", "Citizens on Strike!",JOptionPane.INFORMATION_MESSAGE);
SoldierCount1-=ArmyNum;
MainMenu=false;
MainMenu=true;
}
}
It works where i putted the MainMenu=false; MainMenu=true; but it doesnt where i used return; i had to put return; because MainMenu=false; MainMenu=true; was not working. Thank you for your time :)
Edit: it usually does not work when I use an if inside an if... hope that helps
From what I can interpret, you probably have some code like this:
while(true) {
// A: Do stuff here
while(MainMenu) {
// B: Stuff before your posted code
if(CitizenLoveNum<=20){
// C: Your posted code
}
}
// D: Do other stuff here
}
If you want to go to B, use continue;.
If you want to go to D, use break;.
If you need to go to A without touching D, then enclose D in an if statement.
If your program is ending when you use continue, you probably are missing the while(true) at the top. This part is necessary to keep your code running unconditionally, forever. Otherwise, upon exiting while (MainMenu), your program will run to completion.
Also, I would like to add that
MainMenu = false;
MainMenu = true;
Is equivalent to
MainMenu = true;
Your code executes sequentially. Once you are inside the while loop, MainMenu is not checked again until you encounter a continue or all the code inside the loop finishes executing.
If you want to return to the top of the while body, you can use the keyword
continue;
Changing the MainMenu value in the while body will not cause any action becuase it gets checked before every new loop, the continue keyword will be applied immediately.
This:
MainMenu=false;
MainMenu=true;
Is the same as this:
MainMenu=true;
You're assigning a value that will be checked when it gets back to the start of the loop. If the value is true at that point, it will continue. So if you want to continue looping, set it to true. If you want to stop, just set it to false. Do one or the other, not both. Note that MainMenu=false will continue executing the current loop, and (unless it later gets set to true) will stop it from looping again.
What the return statement does is exits the function that your while loop is enclosed in entirely. If you had cleanup code after the end of your loop, you wouldn't want to return, as that would skip it.
If you want to execute more code after the loop (not exit the function yet), but you want to immediately exit the loop without finishing the current iteration, you can use the break statement.
If you want to return to the start of your loop immediately (from anywhere in the loop) and continue executing it, use the continue statement. Whether the loop executes again will depend on the value of MainMenu.
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 9 years ago.
Improve this question
I was wondering if it is a "bad practice" to use a break statement to exit a loop instead of fulfilling the loop condition?
I do not have enough insight in Java and the JVM to know how a loop is handled, so I was wondering if I was overlooking something critical by doing so.
The focus of this question: is there a specific performance overhead?
Good lord no. Sometimes there is a possibility that something can occur in the loop that satisfies the overall requirement, without satisfying the logical loop condition. In that case, break is used, to stop you cycling around a loop pointlessly.
Example
String item;
for(int x = 0; x < 10; x++)
{
// Linear search.
if(array[x].equals("Item I am looking for"))
{
//you've found the item. Let's stop.
item = array[x];
break;
}
}
What makes more sense in this example. Continue looping to 10 every time, even after you've found it, or loop until you find the item and stop? Or to put it into real world terms; when you find your keys, do you keep looking?
Edit in response to comment
Why not set x to 11 to break the loop? It's pointless. We've got break! Unless your code is making the assumption that x is definitely larger than 10 later on (and it probably shouldn't be) then you're fine just using break.
Edit for the sake of completeness
There are definitely other ways to simulate break. For example, adding extra logic to your termination condition in your loop. Saying that it is either loop pointlessly or use break isn't fair. As pointed out, a while loop can often achieve similar functionality. For example, following the above example..
while(x < 10 && item == null)
{
if(array[x].equals("Item I am looking for"))
{
item = array[x];
}
x++;
}
Using break simply means you can achieve this functionality with a for loop. It also means you don't have to keep adding in conditions into your termination logic, whenever you want the loop to behave differently. For example.
for(int x = 0; x < 10; x++)
{
if(array[x].equals("Something that will make me want to cancel"))
{
break;
}
else if(array[x].equals("Something else that will make me want to cancel"))
{
break;
}
else if(array[x].equals("This is what I want"))
{
item = array[x];
}
}
Rather than a while loop with a termination condition that looks like this:
while(x < 10 && !array[x].equals("Something that will make me want to cancel") &&
!array[x].equals("Something else that will make me want to cancel"))
Using break, just as practically any other language feature, can be a bad practice, within a specific context, where you are clearly misusing it. But some very important idioms cannot be coded without it, or at least would result in far less readable code. In those cases, break is the way to go.
In other words, don't listen to any blanket, unqualified advice—about break or anything else. It is not once that I've seen code totally emaciated just to literally enforce a "good practice".
Regarding your concern about performance overhead, there is absolutely none. At the bytecode level there are no explicit loop constructs anyway: all flow control is implemented in terms of conditional jumps.
The JLS specifies a break is an abnormal termination of a loop. However, just because it is considered abnormal does not mean that it is not used in many different code examples, projects, products, space shuttles, etc. The JVM specification does not state either an existence or absence of a performance loss, though it is clear code execution will continue after the loop.
However, code readability can suffer with odd breaks. If you're sticking a break in a complex if statement surrounded by side effects and odd cleanup code, with possibly a multilevel break with a label(or worse, with a strange set of exit conditions one after the other), it's not going to be easy to read for anyone.
If you want to break your loop by forcing the iteration variable to be outside the iteration range, or by otherwise introducing a not-necessarily-direct way of exiting, it's less readable than break.
However, looping extra times in an empty manner is almost always bad practice as it takes extra iterations and may be unclear.
In my opinion a For loop should be used when a fixed amount of iterations will be done and they won't be stopped before every iteration has been completed. In the other case where you want to quit earlier I prefer to use a While loop. Even if you read those two little words it seems more logical. Some examples:
for (int i=0;i<10;i++) {
System.out.println(i);
}
When I read this code quickly I will know for sure it will print out 10 lines and then go on.
for (int i=0;i<10;i++) {
if (someCondition) break;
System.out.println(i);
}
This one is already less clear to me. Why would you first state you will take 10 iterations, but then inside the loop add some extra conditions to stop sooner?
I prefer the previous example written in this way (even when it's a little more verbose, but that's only with 1 line more):
int i=0;
while (i<10 && !someCondition) {
System.out.println(i);
i++;
}
Everyone who will read this code will see immediatly that there is an extra condition that might terminate the loop earlier.
Ofcourse in very small loops you can always discuss that every programmer will notice the break statement. But I can tell from my own experience that in larger loops those breaks can be overseen. (And that brings us to another topic to start splitting up code in smaller chunks)
Using break in loops can be perfectly legitimate and it can even be the only way to solve some problems.
However, it's bad reputation comes from the fact that new programmers usually abuse it, leading to confusing code, especially by using break to stop the loop in conditions that could have been written in the loop condition statement in the first place.
No, it is not a bad practice to break out of a loop when if certain desired condition is reached(like a match is found). Many times, you may want to stop iterations because you have already achieved what you want, and there is no point iterating further. But, be careful to make sure you are not accidentally missing something or breaking out when not required.
This can also add to performance improvement if you break the loop, instead of iterating over thousands of records even if the purpose of the loop is complete(i.e. may be to match required record is already done).
Example :
for (int j = 0; j < type.size(); j++) {
if (condition) {
// do stuff after which you want
break; // stop further iteration
}
}
It isn't bad practice, but it can make code less readable. One useful refactoring to work around this is to move the loop to a separate method, and then use a return statement instead of a break, for example this (example lifted from #Chris's answer):
String item;
for(int x = 0; x < 10; x++)
{
// Linear search.
if(array[x].equals("Item I am looking for"))
{
//you've found the item. Let's stop.
item = array[x];
break;
}
}
can be refactored (using extract method) to this:
public String searchForItem(String itemIamLookingFor)
{
for(int x = 0; x < 10; x++)
{
if(array[x].equals(itemIamLookingFor))
{
return array[x];
}
}
}
Which when called from the surrounding code can prove to be more readable.
There are a number of common situations for which break is the most natural way to express the algorithm. They are called "loop-and-a-half" constructs; the paradigm example is
while (true) {
item = stream.next();
if (item == EOF)
break;
process(item);
}
If you can't use break for this you have to repeat yourself instead:
item = stream.next();
while (item != EOF) {
process(item);
item = stream.next();
}
It is generally agreed that this is worse.
Similarly, for continue, there is a common pattern that looks like this:
for (item in list) {
if (ignore_p(item))
continue;
if (trivial_p(item)) {
process_trivial(item);
continue;
}
process_complicated(item);
}
This is often more readable than the alternative with chained else if, particularly when process_complicated is more than just one function call.
Further reading: Loop Exits and Structured Programming:
Reopening the Debate
If you start to do something like this, then I would say it starts to get a bit strange and you're better off moving it to a seperate method that returns a result upon the matchedCondition.
boolean matched = false;
for(int i = 0; i < 10; i++) {
for(int j = 0; j < 10; j++) {
if(matchedCondition) {
matched = true;
break;
}
}
if(matched) {
break;
}
}
To elaborate on how to clean up the above code, you can refactor, moving the code to a function that returns instead of using breaks. This is in general, better dealing with complex/messy breaks.
public boolean matches()
for(int i = 0; i < 10; i++) {
for(int j = 0; j < 10; j++) {
if(matchedCondition) {
return true;
}
}
}
return false;
}
However for something simple like my below example. By all means use break!
for(int i = 0; i < 10; i++) {
if(wereDoneHere()) { // we're done, break.
break;
}
}
And changing the conditions, in the above case i, and j's value, you would just make the code really hard to read. Also there could be a case where the upper limits (10 in the example) are variables so then it would be even harder to guess what value to set it to in order to exit the loop. You could of course just set i and j to Integer.MAX_VALUE, but I think you can see this starts to get messy very quickly. :)
No, it is not a bad practice. It is the most easiest and efficient way.
While its not bad practice to use break and there are many excellent uses for it, it should not be all you rely upon. Almost any use of a break can be written into the loop condition. Code is far more readable when real conditions are used, but in the case of a long-running or infinite loop, breaks make perfect sense. They also make sense when searching for data, as shown above.
If you know in advance where the loop will have to stop, it will probably improve code readability to state the condition in the for, while, or `do-while loop.
Otherwise, that's the exact use case for break.
break and continue breaks the readability for the reader, although it's often useful.
Not as much as "goto" concept, but almost.
Besides, if you take some new languages like Scala (inspired by Java and functional programming languages like Ocaml), you will notice that break and continue simply disappeared.
Especially in functional programming, this style of code is avoided:
Why scala doesn't support break and continue?
To sum up: break and continueare widely used in Java for an imperative style, but for any coders that used to practice functional programming, it might be.. weird.
I'm working on a java project in Netbeans. I have a for loop which runs 29 times and each iteration takes about 1 minute to complete. The problem is in the 29th iteration of the loop. Is there any way that I can SKIP the first 28 iterations and go directly to the one in question?
I know I can put a conditional breakpoint, but that dosent make the debugger skip the iterations, it just notifies me when a paticular iteration is reached.
Please Help! Otherwise, this would take a awful lot of time to debug!
You could use something like the Java Platform Debugger Architecture. That might help. On the other hand, you could do something like so:
for (int i = 0; i < ...; i++)
{
if (i == 28)
{
System.out.println("Line Found"); //put breakpoint here
}
//remainder of the code.
}
This should allow you to trigger a breakpoint on the 29th execution of the loop and you can then use the step functions offered by the debugger to go over the code for the 29th iteration.
I have never used the JPDA, and even if I did I think that the most simple and straight forward solution would be to do something like the code above.
You could put a temporary line of code inside your loop, and put a breakpoint on that:
if (i=29) {
// Put a breakpoint on this line (only hit if i=29)
}
Change the loop condition.
for (int index = 27; index < 28; index++) {
...
}
The following is part of the code for my college assignment.
else if (!codeList.contains(userCode)) {
i--; // i is the counter for the for-loop
}
else if (userQuantity[i]==0) {
i--;
}
The first part makes sure that if the user enters the wrong code, the counter i does not increment 1, or rather, it subtracts 1 from the recently incremented counter. This part works fine.
The second part however is what I seem to be having problems with. userQuantity[] is an int array and it has to be an array. This does not seem to do anything to the code. Even if 0 is entered for the quantity, it still incrememnts the counter which is not desireable.
I should explain, to avoid confusion, that this is an infinite for-loop (with a break statement). The reason I am doing a for-loop is because I am required to. Is it because of my for-loop that the condition isn't working or am I doing something completely wrong with it?
This is for my college assignment so I would appreciate answers with explanation and not just quick-fixes. If you need me to explain, let me know please.
Although it's not strictly illegal in Java, it's not a good idea to change the value of the for loop control variable from within the loop. (Such modification is illegal in some other languages.)
By changing the loop iteration variable within the loop, you're messing with the implicit assumptions offered by your use of a for loop. For example, if a reader sees:
for (int i = 0; i < 10; i++) {
// ...
}
the reader will rightfully assume that the loop is intended to execute exactly 10 times (or, no more than 10 if there is a break in there). However, if you go changing the value of i within the loop, this assumption is no longer valid.
If you must change the value of the counter, I would suggest writing this as a while loop instead:
int i = 0;
while (i < 10) {
// ...
i++;
}
along with a comment that explains why you are changing i within the loop and what it means to do so.
This is very bad practice. Change the for loop to a while loop and only increment if
codeList.contains(userCode)==true or userQuantity[i]!=0.
I should explain, to avoid confusion, that this is an infinite for-loop (with a break statement). The reason I am doing a for-loop is because I am required to. Is it because of my for-loop that the condition isn't working or am I doing something completely wrong with it?
I have a feeling that you are misunderstanding the requirements (e.g. you are not required to use a for loop), or that there is a mistake in your thinking; i.e. there is a simpler solution that doesn't involve the counter going backwards.
(It is surprising that a programming exercise would require you to write code that most experienced Java programmers would agree is bad code. The simple explanation is that it is not.)
Either way:
Changing the loop variable in a for loop is bad practice, for the reasons described by Greg.
The idea of an "infinite for loop" is really strange. The following is legal Java ...
for (int i = 0; true; i++) {
...
}
but the idiomatic way to write it is:
int i = 0;
while (true) {
...
i++; // ... at the appropriate point / points
}
... which in most cases means that you don't need to make the variable go backwards at all.