Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pausing a for loop in java

Tags:

java

loops

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.

like image 363
eBehbahani Avatar asked Dec 12 '11 04:12

eBehbahani


People also ask

How do I stop a Java execution?

In Java exit() method is in java. exit() method terminates the current JVM running on the system which results in termination of code being executed currently.

How do you skip while in Java?

The continue statement skips the current iteration of a loop ( for , while , do... while , etc). After the continue statement, the program moves to the end of the loop.

Why does my while loop not stop Java?

The issue with your while loop not closing is because you have an embedded for loop in your code. What happens, is your code will enter the while loop, because while(test) will result in true . Then, your code will enter the for loop. Inside of your for loop, you have the code looping from 1-10.


2 Answers

When iterating through a for loop, for example, the one below, it does the following

for (int i = 0; i < 6; i++) {
    // Do stuff
}
  1. It declares the variable i and assigns a value of 0.
  2. It checks the conditional i < 6. If true, then proceed to step 3. Otherwise go to step 6.
  3. Goes through the body of the loop.
  4. Increment the variable i due to the i++ in the for loop header.
  5. Go to step 2.
  6. 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.

like image 104
Dan Avatar answered Sep 23 '22 01:09

Dan


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.

like image 29
Jon Skeet Avatar answered Sep 26 '22 01:09

Jon Skeet