-4

Why does this code take not run?

for(int i=0;i<11;i++){
    for(int j=0;j<11;j++){  

        if(i>0){
            if((staticWallLoc[i--][j]&4)>0){staticWallLoc[i][j]=staticWallLoc[i][j]|1;}
        }
        if(j<10){
            if((staticWallLoc[i][j++]&8)>0){staticWallLoc[i][j]=staticWallLoc[i][j]|2;}
        }
        if(i<10){
            if((staticWallLoc[i++][j]&1)>0){staticWallLoc[i][j]=staticWallLoc[i][j]|4;}
        }
        if(j>0){
            if((staticWallLoc[i][j--]&2)>0){staticWallLoc[i][j]=staticWallLoc[i][j]|8;}
        }

        System.out.println(i+" "+j);
    }
}

By the way, staticWallLoc[11][11] is a two dimensional array that has 11 indexes in each dimension (0-10).

Just wondering why my program literally cannot get passed this code.

Ben Hagel
  • 441
  • 4
  • 9
  • 22

7 Answers7

2

Dude - why are you decrementing in the loop body the same variables you're using to control the loop counter?!?

Sounds like "one step forward, two steps back", doesn't it ;)?

SUGGESTION:

Run the debugger, single step through a few iterations, and carefully note what happens to "i" and "j".

How soon do you think either or both will reach "11" ;)?

paulsm4
  • 114,292
  • 17
  • 138
  • 190
1

Inside your if(i > 0), i is decremented by staticWallLoc[i--]. Change it to staticWallLoc[i-1], and do the same for j.

if(i>0){
    // Try something like this instead
    if((staticWallLoc[i-1][j]&4)>0){
      staticWallLoc[i][j]=staticWallLoc[i][j]|1;
    }
}

You will also want to change your [i++] to [i+1].

Keldon Alleyne
  • 2,103
  • 16
  • 23
  • Thanks, I made a really really stupid mistake, I'm just used to using -- and ++, im a first time programmer, so these stupid mistakes are seldom but, I still make them! XD thanks! – Ben Hagel Jul 31 '12 at 02:06
  • Not a problem. Mistakes like these provide valuable experience. Now when you read a theoretical book you will at least have the benefit of having this memorable occasion to better understand the reasoning behind various programming practices, etc. – Keldon Alleyne Jul 31 '12 at 05:03
0

In your loop, you have statements like if (i>0) {staticWallLoc[i--][j].... These are directly altering the loop index. So, when i is 1, it is changed back to 0. When the loop is incremented, it goes back to 1, and then 0 again, creating an infinite loop.

Do you perhaps mean to use staticWallLoc[i - 1][j]..., etc. ? This gets the desired index without changing the loop index.

wquist
  • 2,569
  • 1
  • 15
  • 14
0

rather than using i++ i-- etc in your code, just use offsets i-1 or i+1.

General rule of thumb: in for loops, don't mess with the for loop variables within the body.

(there are exceptions to the rule, but this doesn't look like an exceptional case)

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
0

Without knowing what the heck the contents of your array is it's hard to say. Might it be because you're modifying the values of i and j on the fly with -- and ++? Do you mean to be keeping those values constant and doing something like:

if((staticWallLoc[i+1][j]&1)>0){staticWallLoc[i][j]=staticWallLoc[i][j]|4;} // changed i++ to i+1
Kaleb Brasee
  • 51,193
  • 8
  • 108
  • 113
0

Once j reaches 10, it does not get incremented in the "if (j < 10)" section, but it gets decremented in the "if (j > 0)" section, so j will end the loop at 9, then get incremented back to 10 by the loop management. So you will have an infinite loop with i = -1/0 (since it gets decremented and incremented in the loop) and j = 9/10 (for the same reason).

David
  • 1,429
  • 8
  • 8
0

You do a j-- in the last if once j >=10. The loop will stuck at j==10.

It's not a very good idea to manipulate the loop variable :)

Csongor Fagyal
  • 514
  • 2
  • 10