user3144079
user3144079

Reputation: 159

My nested for loop isn't executing and I don't know why

My simple nested for loop isn't executing. This piece of code is located in a method. The method itself is executing and the code around it is executing but the nested loop isn't executing. I tried copying and pasting the code in a different area in the same method but it didn't work. I tried to create a different class unrelated to this project and pasting it there with neccessary adjustments and it didn't work.

All other neccessary variables and number information is viewable in the code and the results provided.

      System.out.println("This is just to show that variables do have actual numbers in them." +
                       "\n  basePointy =  " + basePointy + "\n  basePointx = " + basePointx + 
                       "\n  boardMatrix.length = " + boardMatrix.length + "\n  boardMatrix[0].length = " + boardMatrix[0].length + 
                       "\n  a[0][0] = " + a[0][0] + "\n  a[5][5] = " + a[5][5] + "\n");

      System.out.println("This is before the nested for loop");

      //This is the problem area.
      for(int yaxisCounter = basePointy + 185; yaxisCounter < boardMatrix.length; yaxisCounter++)
      {
         System.out.println("This is in the first part of the nested for loop");
         for(int xaxisCounter = basePointx + 221; xaxisCounter < boardMatrix[0].length; xaxisCounter++)
         {
            boardMatrix[yaxisCounter-basePointy-185][xaxisCounter-basePointx-221] = a[yaxisCounter][xaxisCounter];    
            System.out.println("This is in the second part of the nested for loop");
         } 
      }
      System.out.println("This is after the nested for loop");

This is the result I got after running the code.

This is just to show that variables do have actual numbers in them.
  basePointy =  160
  basePointx = 46
  boardMatrix.length = 16
  boardMatrix[0].length = 300
  a[0][0] = -8345659
  a[5][5] = -8412480

This is before the nested for loop

This is after the nested for loop

Notice that it doesn't printout "This is in the first part of the nested for loop." and "This is in the second part of the nested for loop." but prints/executes everything before and after the nested for loop.

Thank you in advance for people that read and for people were able to help

Upvotes: 0

Views: 66

Answers (3)

Kamlesh Kumar
Kamlesh Kumar

Reputation: 1680

because first loop condition is false

Upvotes: 1

outdev
outdev

Reputation: 5492

Try this

for (int yaxisCounter = basePointy + 185; yaxisCounter < boardMatrix.length; yaxisCounter++) {
    System.out.println("This is in the first part of the nested for loop");
    for (int xaxisCounter = basePointx + 221; xaxisCounter < boardMatrix[yaxisCounter].length; xaxisCounter++) {
        boardMatrix[yaxisCounter - basePointy - 185][xaxisCounter - basePointx - 221] = a[yaxisCounter][xaxisCounter];
        System.out.println("This is in the second part of the nested for loop");
    }
}

Upvotes: 0

SMA
SMA

Reputation: 37023

Because when you start your outer loop, your yaxisCounter = 160 + 185 = 345, and your loop condition is yaxisCounter < boardMatrix.length which is false as 16 is not greater than 345 (i.e. 345 < 16)

Upvotes: 1

Related Questions