1

I'm currently making a program where nested loops are needed to search through an array to find a spot for good input within the array. Here is an example:

public void placePiece(int column) {
        boolean goodInput = false;

        while(!goodInput) {
            for(int x = 5; x >= 0; x--) {
                if(boardStatus[x][column] == 0) {

                    setRow(x);
                    boardStatus[x][column] = 1;
                    goodInput = true;
                    break;

                }else if(boardStatus[0][column] == 1) { 
                    goodInput = false;
                    break;
                }else{

                }

            }
        }
    }

The method takes a parameter which is the column in which the piece should be located (received by a mouse listener). If the column in the 2D array is already filled to the top, the program gets stuck in an endless loop within the "else if", and I'm unsure how I would break out of this loop. How could I break out of this loop if there is bad input so that the user can try to give another column as input.

AlecR
  • 57
  • 7

5 Answers5

8

An easy way is to use a labeled break statement.

myLabelName:
for(outer loop)
    for(inner loop)
        if(condition)
            break myLabelName;

This is useful when you'd rather not waste time iterating over other objects/items when you've found what you needed.


To expand, when you use just break; (without a label) it will exit the parent loop. Ex:

myLabelName:
for(outer loop){
    for(inner loop){
        if(condition){
            break myLabelName; //breaks outer loop
        }
        else if(other condition){
            break; //breaks parent (inner/nested) loop
        }
     }
 }
CubeJockey
  • 2,209
  • 8
  • 24
  • 31
0

Change the parameter AFTER the loop:

public void placePiece(int column) {
    boolean goodInput = false;

    while(!goodInput) {
        for(int x = 5; x >= 0; x--) {
            if(boardStatus[x][column] == 0) {

                setRow(x);
                boardStatus[x][column] = 1;
                goodInput = true;
                break;

            }else if(boardStatus[0][column] == 1) { 
                goodInput = false;
                break;
            }else{

            }

        }
        goodInput = true; // <-----
    }
}
CubeJockey
  • 2,209
  • 8
  • 24
  • 31
Fundhor
  • 3,369
  • 1
  • 25
  • 47
0

When you are in elseif loop your are again assigning the goodInput = false; so the while conditions again becomes true and loops continuous forever.

if you want to take input till correct input is received can use do(){}while() loop

Ankur Anand
  • 3,873
  • 2
  • 23
  • 44
0

Based on the description you provided, I am not sure of the purpose of the else if statement. As I understand you intention, you are checking all of the rows within a certain column to see if that cell has been set or not?

The combination of the while loop and the for loop seems suspect as well. There doesn't seem to be any need for the while loop in this case.

What is supposed to happen if the entire column is filled? Should this method return a status flag?

Can't this be implemented with a single for loop?

for (int x = 5; x >= 0; --x)
{
    if(boardStatus[x][column] == 0) {
        setRow(x);
        boardStatus[x][column] = 1;
        break;
    }
}

If you want, you can track a flag variable to indicate if you ever found a cell which was empty.

GLNN.LRSN
  • 56
  • 1
  • 9
0

you can use flag or you can use goto statement

flag description

code

bool  flag = true;
for (int i = 0; (i < 10) && (flag==true); i++)
{
   for(int j = 0; (j < 10) && (flag==true); j++)
   {
      if (condition)
         flag = false;
   }  
}
Harish
  • 425
  • 7
  • 22