Break a loop for java

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
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
            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
  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++) {;

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.

Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » Break a loop for java

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录