ajkey94
ajkey94

Reputation: 431

error: bad operand types for binary operator '||'

This is my statement

while(coord.getRow() || coord.getColumn() != 0)

I basically want it to loop while the x coordinate and y coordinates are not 0. then stop executing when either equals 0. Both the row and column of the object cord are ints.

Like the title I'm receiving the error:

error: bad operand types for binary operator '||'

I could I go about executing a while loop to do something like this?

Upvotes: 2

Views: 7465

Answers (3)

William Gaul
William Gaul

Reputation: 3181

Try:

while((coord.getRow() != 0) && (coord.getColumn() != 0)) {
    // etc...
}

You have to use && here instead of ||, because you want to stop when either equals 0, so in your while case you have to make sure both are not zero.

The reason for the error is that logical operators like || and && in Java expect logical expressions. In other words, something that can evaluate to true or false.

Since your functions return integers, these are not logically valid. Whereas, something like coord.getRow() != 0 is either true or false depending on what coord.getRow() is.

Upvotes: 2

Trying
Trying

Reputation: 14278

use:

while(coord.getRow() != 0 && coord.getColumn() != 0)

use && (and) operator instead of || (or).

&& is true if both true on the other hand || is true if any one is true.

Upvotes: 0

newuser
newuser

Reputation: 8466

while(coord.getRow() != 0 && coord.getColumn() != 0)

instead of

while(coord.getRow() || coord.getColumn() != 0)

Upvotes: 2

Related Questions