Problem with ||

The Java ‘or’ statement || isn’t accepted when I try to run code with it. I 'm using two straight line signs at top right of my Spanish keyboard - is there some other symbol I can use?

The two straight lines are called pipes, and they are the defacto symbol for logical OR.

A || B

where A and B are expressions.

Written as it appears above will have no effect, but as a statement, the entire expression is a boolean.

if (A || B) {}

while (A || B) {}

C = A || B;

Thanks Roy. I’m quite aware of what the two lines (‘pipes’, thanks) are supposed to do, it’s just that an error was flagged when I input them using my keyboard. I’ve found a way around this by deleting the first pipe then entering another - all a bit bizarre, but solved.

1 Like

One should qualify the statement from earlier, “the entire expression is a boolean”. This is not exactly true in all cases.

2 || 'two'    // -> 2

2 && 'two'    // -> 'two'

That’s something I often overlook. Sorry for the confusion.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.