Suspending a for loop in java

Is there a way to pause a for loop in java? So, is there a way to just go to one iteration when prompted? I read this http://answers.yahoo.com/question/index?qid=20100212201605AAazS73 , and the solution seems to have some problems, mainly because I don’t quite understand the order that the loop checks the header. The only method I could think of that could do something like this is as follows

do { if (FLAG) { //Do procedure i++; FLAG = false; } } while ( i < 6); 

When the flag is correct, the procedure is executed, and the counter moves forward. I don’t like it, because it will continue to cycle if the counter is less than 6, if I’m not mistaken. Any ideas?

- Sorry for the lack of clarity. FLAG in my case will be a static Boolean that can be called from another class. The procedure I'm talking about depends on i.

+4
source share
6 answers

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

 for (int i = 0; i < 6; i++) { // Do stuff } 
  • It declares the variable i and assigns the value 0 .
  • It checks conditional i < 6 . If true, go to step 3. Otherwise, go to step 6.
  • The body of the cycle passes.
  • Increase i due to i++ in the loop header.
  • Go to step 2.
  • The circuit is completed.

As for your goal, I'm not sure what your goal is. Do you want to pause the use of the lock method? If so, something like this will work:

 for (int i = 0; i < 6; i++) { System.in.readLine(); } 

Alternatively, you can use some kind of flag that checks if the loop should work, for example:

 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 helps.

+3
source

It’s not clear what this “hint” is. You could 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 } 

This is suitable for a console application, but obviously not for (say) a Swing application. It also does not apply to the FLAG part of your sample code, because it is not clear what this means. Are you trying to request additional information from the user or just confirmation to continue? If you could clarify what you are trying to achieve, this will really help.

To check if this is for code without throwing away, you can extract the idea of ​​a user request so that you can test with an implementation that does not actually request the user, but simply writes that it would do this.

+2
source

This will result in a Thread lock that takes up the for loop.

You can do it simply, but not very well, with this:

 for(something) while(!FLAG) //Do procedure 

Another way would be to make another Thread , and the main thread will wait for this other thread.

Here's some more info: How do I get a Java thread to wait for another thread to exit?

0
source

Your goal is somewhat unclear. I think you want your program to continue to work until you get six from a certain input, and if so, this approach will work, although, of course, you will need to get input from the user to move the loop forward.

If you are worried that the while loop will use many system resources, this will not be a problem.

0
source

Ok, you could use Thread.Sleep(); to pause a bit for checking flags, but what you are really looking for is a function that blocks while waiting for input, System.in.readline(); blocks, if I remember correctly;)

 int i = 0 do { if(FLAG) { //Do stuff i++; //Clear Flag } Thread.Sleep(50); //Sleep for 50 ms } while (i < 6); 

or so:

 for(int i = 0; i < 6; i++) //Execute readline 6 times. { System.in.readLine(); } 
0
source

Others examined how to use System.in.readLine() so that the program explicitly required action from the user.

If you need the program to not expect the user, but let you, the programmer, slow down the program so that you can find and fix the error, you may want to use a debugger, because this is exactly what it is intended for.

Every modern Java IDE has a debugger. The keys selected to use it simply change.

If you use Eclipse, you use F11 or Ctrl-F11 to run your program (assuming that Windows). The difference is that F11 runs your program inside the debugger, but Ctrl-F11 does not.

Place the cursor on the first line inside the for loop and select Run-> Toggle Breakpoint. A blue bullet will appear to the left of the line. This indicates that the breakpoint is active - the debugger now stops your program every time it reaches this line.

Now run your program in the debugger using F11. The program stops at the line, and you can examine your variables in the Variables panel as needed and continue execution with F8 whenever you are ready.

0
source

Source: https://habr.com/ru/post/1385829/


All Articles