5

I've got a problem, I'm getting a "Dead Code" warning in Eclipse and I really don't know why. The code is from my Connect Four project, to be more precise it's from the Class that checks if somebody has won. This method checks all the horizontal winning possibilities for red. The code is the following:

/**
 * Method to check the horizontal winning possibilities for red
 * @return true if red won or false if not
 */
public boolean checkHorRed(){
    for(int line = 0; line < 6; line++) {
        for(int column = 0; column < 4; column++) { //column++ is underlined and causes the "dead Code" warning
            if(gw.buttons[line][column].getIcon().equals(gw.red));
                if(gw.buttons[line][column+1].getIcon().equals(gw.red));
                    if(gw.buttons[line][column+2].getIcon().equals(gw.red));
                        if(gw.buttons[line][column+3].getIcon().equals(gw.red));
                            return true;
        }
    }
    return false;
}    

The game is even caused to be immediately won because of this method. What's strange about this is that all the other methods in the class that look almost the same don't cause any problems. Here's the method that checks the vertical winning possibilities for yellow, to have a comparison:

/**
 * Method to check the vertical winning possibilities for yellow
 * @return true or false
 */
public boolean checkVertYel(){
    for(int line = 3; line < 6; line++) {
        for(int column = 0; column < 7; column++) {
            if(gw.buttons[line][column].getIcon().equals(gw.yellow))
                if(gw.buttons[line-1][column].getIcon().equals(gw.yellow))
                    if(gw.buttons[line-2][column].getIcon().equals(gw.yellow))
                        if(gw.buttons[line-3][column].getIcon().equals(gw.yellow))
                            return true;
        }
    }
    return false;
}    

This one does not cause any problems. Can somebody tell me where the warning comes from? If you need additional information please tell me.

Lunaetic
  • 229
  • 2
  • 9
  • 1
    As it stands all of those `if`s aren't doing anything because you've got a `;` after each one. That means your inner `for` loop **always** returns true on the first iteration - meaning the `column++` can never be reached. This is why you should always use `{}`, even with simple loops/ifs. – JonK Oct 30 '14 at 09:07
  • 1
    You should think of using `&&` operator in your `if`statements. – jhamon Oct 30 '14 at 09:09

3 Answers3

1

The dead code in your function is the increment statement of your inner for loop (column++). The return true statement will always be executed (if the loop is executed), so the loop increment is never going to happen.

That is your code, but properly formatted:

// ...

for(int column = 0; column < 4; column++) {
    //column++ is underlined and causes the "dead Code" warning
    if(gw.buttons[line][column].getIcon().equals(gw.red));

    if(gw.buttons[line][column+1].getIcon().equals(gw.red));

    if(gw.buttons[line][column+2].getIcon().equals(gw.red));

    if(gw.buttons[line][column+3].getIcon().equals(gw.red));

    return true;
}

// ...

You can easily spot the error: return true will always be executed, so the increment statement of the inner loop will not be executed.

That is how you code should look like:

public boolean checkHorRed() {
    for(int line = 0; line < 6; line++) {
        for(int column = 0; column < 4; column++) {
            //column++ is underlined and causes the "dead Code" warning
            if(gw.buttons[line][column].getIcon().equals(gw.red)
                    && gw.buttons[line][column+1].getIcon().equals(gw.red)
                    && gw.buttons[line][column+2].getIcon().equals(gw.red)
                    && gw.buttons[line][column+3].getIcon().equals(gw.red) {
                return true;
            }
        }
    }

    return false;
}
ifloop
  • 8,079
  • 2
  • 26
  • 35
  • Okay, your answer is even more detailled so I will accept this one. Thanks for all the answers to my really stupid problem! – Lunaetic Oct 30 '14 at 09:16
0

In the above method you have ; after each if statements, where as your second method is correct, which is proper way.

  if(gw.buttons[line][column].getIcon().equals(gw.red)); <--

That terminates the if there it self. Your line of code equivalent to

   if(condition)
    {

    }

Which means the code after the if conditions is dead.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

This is your code after reformatting it:

public boolean checkHorRed() {
    for (int line = 0; line < 6; line++) {
        for (int column = 0; column < 4; column++) { //column++ is underlined and causes the "dead Code" warning
            if (gw.buttons[line][column].getIcon().equals(gw.red)) {
                ;
            }
            if (gw.buttons[line][column + 1].getIcon().equals(gw.red)) {
                ;
            }
            if (gw.buttons[line][column + 2].getIcon().equals(gw.red)) {
                ;
            }
            if (gw.buttons[line][column + 3].getIcon().equals(gw.red)) {
                ;
            }
            return true; //this will always happen
        }
    }
    return false;
}

And this is the other:

public boolean checkVertYel() {
    for (int line = 3; line < 6; line++) {
        for (int column = 0; column < 7; column++) {
            if (gw.buttons[line][column].getIcon().equals(gw.yellow)) {
                if (gw.buttons[line - 1][column].getIcon().equals(gw.yellow)) {
                    if (gw.buttons[line - 2][column].getIcon().equals(gw.yellow)) {
                        if (gw.buttons[line - 3][column].getIcon().equals(gw.yellow)) {
                            return true;
                        }
                    }
                }
            }
        }
    }
    return false;
}

Basically, you should really not end your if statements with semicolons.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428