Confused on if statements condition...
Hello All,
I have a if statement that uses the or ( '|' ) and the and ( '&' ) for the if's condition. Here is the code:
Code :
if (ch < '1' | ch > '7' & ch != 'q') return false;
else return true;
So if I'm reading this right, it says ch less than '1' OR ch greater than '7' AND ch not 'q'...
Well when 'ch' is 'q' this code returns false... I just don't understand because it seems like the (ch != 'q') would have to be true if this statement is going to return false...
Could someone explain why this works?
I appreciate any feedback!
Re: Confused on if statements condition...
To use the AND and OR operators in java, you need to write them as (&&) and ( || ).
Re: Confused on if statements condition...
You should use ()s around the expressions to show what order you want them evaluated in and how they should be connected. Look at a table of operator precedences to see what order the expressions are evaluated.
What you have posted makes it hard for anyone to easily see how the expressions will be evaluated. With ()s it would be clear.
Re: Confused on if statements condition...
Quote:
Originally Posted by
Discoveringmypath
...
Well when 'ch' is 'q' this code returns false...
Really? Says who? Did you test it?
Code java:
//
// From Zaphod_b
//
public class Z {
public static void main(String [] args) {
char [] ch = {'0', '3', '9', 'q'};
for (int i = 0; i < ch.length; i++) {
System.out.println("Calling foo('" + ch[i] + "')");
boolean x = foo(ch[i]);
System.out.println("Result is " + x + "\n");
}
}
static boolean foo(char ch) {
System.out.print(" ch < '1' : ");
System.out.println((ch<'1'));
System.out.print(" ch > '7' : ");
System.out.println(ch>'9');
System.out.print(" ch != 'q' : ");
System.out.println(ch != 'q');
if (ch < '1' | ch > '7' & ch != 'q') {
return false;
}
else {
return true;
}
}
}
Output:
Code :
Calling foo('0')
ch < '1' : true
ch > '7' : false
ch != 'q' : true
Result is false
Calling foo('3')
ch < '1' : false
ch > '7' : false
ch != 'q' : true
Result is true
Calling foo('9')
ch < '1' : false
ch > '7' : false
ch != 'q' : true
Result is false
Calling foo('q')
ch < '1' : false
ch > '7' : true
ch != 'q' : false
Result is true
Quote:
Originally Posted by
Discoveringmypath
I just don't understand...
The first key to understanding is to make sure what you "know" is correct. (Mark Twain reportedly said something like "It's not what you don't know that kills you. It's what you 'know' that is just flat wrong.") For the codelet you posted, if ch is equal to 'q' the code returns true.
Quote:
Originally Posted by
Discoveringmypath
...explain...
The second key to understanding expressions with multiple terms and several different operators is to use parentheses around terms to emphasize the precedence rules. (Put parentheses around terms connected with highest precedence operators and work your way out from there.)
Reading about precedence rules for Java and following Norm's suggestions about using parentheses to emphasize the precedence for human readers leads me to the following conclusion:
The expression in the parentheses is true if
(ch is less than '1') OR ((ch is greater than '7') AND (ch is not equal to 'q'))
The function returns the boolean inverse of the value of the expression, i.e. it returns true of the expression is false and returns false if the expression is true.
Now, look at the output and see if it makes sense in the light of the rules of the Java programming language..
And, by the way, the single '|' and '&' operators work the way that you probably think they should for boolean variables, but there are reasons to use the double '||' and '&&' operators.
In your program the results are the same, but you may find some examples in your text or class notes or whatever material that you are learning from that point out differences, and point out why the "doubles" might be preferred in many cases.
Unlike the situation with boolean variables, for integer data types the results from the '|' and '&' operators are very different from corresponding results of the '||' and '&&' operators, so, for a given functionality you would have no choice.
Cheers!
Z
Re: Confused on if statements condition...
Hello Zaphod_b,
Thanks for the reply, you hit it on the nose. I was looking at it wrong, it doesn't return false when I input 'q'. I was mistaken. I ended up looking back at the code and realized that when I press 'q' it would return true.
The return goes to a while loop, and in order to get out of the loop I needed it to return 'true' because of the well placed '!' in the while loops condition. So true would flip to false and break the loop. Thanks for the feedback though, I'm new to Java programming. I figured it out when I was at work because it was bothering me that I didn't understand it.
Thanks again!