0

I have a question about logical operators. I've written some code for Pset3, Game-of-Fifteen, as part of the "MOVE" function. This code aims to determine whether "tile" is in a "legal" position to move, which is determined by its relative position with respect to "cursor" (for those unfamiliar with the problem set, the "tile" is an int in an array of size [d],[d], which is input by the user; the "cursor" is the value of 0 within the array. There are four positions in which the "tile" can be said to be in a "legal" position with respect to the "cursor" - these are set out in the logical conditions set out on each line of my code, separated by the OR statement. Here is the relevant code:

    if
    (
        ((tile_row == cursor_row-1) && (tile_column == cursor_column))
        ||
        ((tile_row == cursor_row)) && ((tile_column == cursor_column-1))
        ||
        ((tile_row == cursor_row)) && ((tile_column == cursor_column+1))
        ||
        ((tile_row == cursor_row+1)) && ((tile_column == cursor_column))
    )
    {
        Do something;
    }
    else
    {
        Do some other thing;
    }

When I compile this code, I get the following error:

fifteen.c:286:40: error: '&&' within '||' [-Werror,-Wlogical-op-parentheses]
        ((tile_row == cursor_row)) && ((tile_column == cursor_column-1))
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fifteen.c:286:40: note: place parentheses around the '&&' expression to silence this warning
        ((tile_row == cursor_row)) && ((tile_column == cursor_column-1))

(In fact I get this repeated three times, corresponding to the 2nd, 3rd, and 4th of the logical conditions).

I am able to "solve" the problem by putting in an additional pair of parentheses around the outside of the statement on each line - in the sense that the code compiles and when run, does what I expect. But I don't understand why I'm getting the error, or why this should solve it! As far as I can see, of the four conditions is complete and surrounded by appropriate parentheses, which are then separated by the OR function. So adding an additional pair of parentheses should not change the logic. Can anyone explain what's going on here? Thanks!

1 Answer 1

1

Instead of

    if
    (
        ((tile_row == cursor_row-1) && (tile_column == cursor_column))
        ||
        ((tile_row == cursor_row)) && ((tile_column == cursor_column-1))
        ||
        ((tile_row == cursor_row)) && ((tile_column == cursor_column+1))
        ||
        ((tile_row == cursor_row+1)) && ((tile_column == cursor_column))
    )

try

    if
    (
        ((tile_row == cursor_row-1) && (tile_column == cursor_column))
        ||
        ((tile_row == cursor_row) && (tile_column == cursor_column-1))
        ||
        ((tile_row == cursor_row) && (tile_column == cursor_column+1))
        ||
        ((tile_row == cursor_row+1) && (tile_column == cursor_column))
    )

(replaced )) && (( with just ) && ()

&& has higher precedence than ||, but as you read a line in a linear way, from left to right, and especially if you're not so much into boolean logic, you might not get what that means, so clang warns when you place && and || on the same level.

1
  • Thanks once again Blauelf. And once again, an unfortunate typo by me! That's what I meant to write (and thought I had written). Which is why I was confused - I thought I'd put the &&s inside double parentheses on all four lines. My goodness but my coding is sloppy and my proof-reading even worse! But at least my formal logic isn't so bad... Jan 31, 2017 at 14:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .