# Explanation of Nested Loop (very strange)

• 11-24-2009, 10:49 PM
Jonotron
[SOLVED] Explanation of Nested Loop (very strange)
Hey, I've been programming for a long time, but I'm new to java (I know it's still very similar to others). I'm having a strange result from a board(like chess) I'm making:

Here is the variable init.
Code:

```    //Board Dimensions     public static final int WIDTH = 20;     public static final int HEIGHT = 10;         //I'm using boolean because it's either a dot or an asterisk...     public static boolean[][] board = new boolean[WIDTH][HEIGHT];```

When I drew the board, the x and y flipped positions for some reason.
Code:

```static void drawBoard()     {       for (int x = 0; x < WIDTH; x++)       {           for (int y = 0; y < HEIGHT; y++)           {               if (board[x][y] == true)               {                   System.out.print( '*' );               }               else               {                   System.out.print( '.' );               }            }           System.out.println();       }     }```
I was able to correct the problem by just reversing it like this:
Code:

```static void drawBoard()     {         //HAVE TO START WITH COLUMN AND INVERSE THE X Y       for (int x = 0; x < HEIGHT; x++)       {           for (int y = 0; y < WIDTH; y++)           {               if (board[y][x] == true) //NEED TO INVERT X AND Y TO MAKE WORK               {                   System.out.print( '*' );               }               else               {                   System.out.print( '.' );               }            }           System.out.println();       }     }```
And the desired output should be similar to "Conway's Game of Life" as this is supposed to be a replica.
• 11-24-2009, 11:20 PM
collin389
Hello, in your first for loop, you print the heights and then make a new line, and then print the next column, I think what you want to do is print the entire width then make a newline, and then print the next widths. Like this:
Code:

``` static void drawBoard()     {       for (int y = 0; y < HEIGHT; y++)       {           for (int x = 0; x < WIDTH; x++)           {               if (board[x][y] == true)               {                   System.out.print( '*' );               }               else               {                   System.out.print( '.' );               }            }           System.out.println();       }     }```
• 11-25-2009, 03:35 AM
Jonotron
Oh wow, now that I think about it, that makes so much more sense. Thanks a lot!
• 11-25-2009, 04:20 AM
collin389