0
if (array[i]<(char)65 || array[i]>(char)122 && array[i]>(char)91 || array[i]<=(char)96) System.out.println("False") 

in this code, when i try to assign character 'C' (which is 67 btw) to array[i] it still says false. I did the math and it's not supposed to print "false" as I stated below this line.

(67 < 65 = 0 || 67 > 122 = 0) = 0 (67 > 91 = 0 || 67 <= 96 = 1) = 1 

So, this leaves us: 0 & 1 = 0 . Any ideas?

6
  • 5
    indentation, formatting, some parentheses, and a semicolon Commented Nov 13, 2013 at 18:12
  • 10
    How do you expect the many && and || operators to interact with each other? Add some bracketing and I suspect you'll be okay... Commented Nov 13, 2013 at 18:13
  • 5
    Logical && has higher precedence. docs.oracle.com/javase/tutorial/java/nutsandbolts/… Commented Nov 13, 2013 at 18:13
  • Ohh so that's why!. Thanks a lot. This has been bothering me for like 5 hours. Commented Nov 13, 2013 at 18:15
  • 3
    Try to use a more descriptive title name next time. People scrolling through questions will have no idea what this question is about without clicking on it. Even something like: "Java boolean expression giving unexpected result" is heaps better Commented Nov 13, 2013 at 18:19

3 Answers 3

3

With some formatting, your code is:

if ( array[i] < (char)65 || array[i] > (char)122 && array[i] > (char)91 || array[i] <= (char)96 ) System.out.println("False"); 

Since && has higher precedence than || (see Operators), this is equivalent to:

if ( array[i] < (char)65 || ( array[i] > (char)122 && array[i] > (char)91 ) || array[i] <= (char)96 ) System.out.println("False"); 

which, since && is short circuiting, is behaviorally equivalent to:

if ( array[i] < (char)65 || array[i] > (char)122 || array[i] <= (char)96 ) System.out.println("False"); 

which, since the last case covers the first, is logically equivalent to:

if ( array[i] > (char)122 || array[i] <= (char)96 ) System.out.println("False"); 

You'll print False whenever the value it greater than 122 or less than or equal to 96. 67 is less than 96, so you print False. As pointed out the in comments, there is a precedence to the operators. Rather than learning all the details (to predict cases like this), it's easier just to use enough parentheses.

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

Comments

1

The && is evaluated first, and then the || according to the Java Operator Precedence. So it gets evaluated the following way for 'C'

67>122 && 67>91 //false 67<65 || false //false false || 67<=96 //true 

if you uses parentheses it will solve this problem

if ((array[i]<(char)65 || array[i]>(char)122) && (array[i]>(char)91 || array[i]<=(char)96)) 

Comments

0

I think you may try this by using brackets ():

if ((array[i]<(char)65 || array[i]>(char)122) && (array[i]>(char)91 || array[i]<=(char)96)) System.out.println("False") 

Also to note that && has higher precedence

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.