0

This is back to the beginning of Java 101, but I have this code here:

    if ((d==3)&&(City.walls[x--][y])){
        System.out.println ("Fourth Condition true");
        System.out.println (City.walls[x--][y]);
        return false;
    }

Even if City.walls[x--][y]) is false, and System.out.println confirms this by printing out false, it will still enter the if statement, no matter what. What am I doing wrong with the comparison? Thanks in advance.

1
  • 3
    That code looks fine to me. Bear in mind, though, that x-- will change the value of x, so whatever x was before the if block, it will be one less inside the block, possibly giving you a misleading debugging statement. Commented Oct 10, 2012 at 4:31

2 Answers 2

3

You are using x--, which changes the value of x. First it returns the value, then it decrements the value:

int x = 5;
System.out.println(x--); //outputs 5
System.out.println(x);   //outputs 4

You probably want to say x-1

Sign up to request clarification or add additional context in comments.

Comments

2

The values will be different when your code executes in if condition and then inside if condition where you are printing in console.

You are decrementing value of x in the if condition. So, When you print it inside condition you will get decremented value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.