Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Confused on if statements condition...

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question 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:

    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!


  2. #2
    Junior Member Mitch1337's Avatar
    Join Date
    Sep 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Confused on if statements condition...

    To use the AND and OR operators in java, you need to write them as (&&) and ( || ).

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Confused on if statements condition...

    Quote Originally Posted by Discoveringmypath View Post
    ...
    Well when 'ch' is 'q' this code returns false...
    Really? Says who? Did you test it?

    //
    // 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:
    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 View Post
    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 View Post
    ...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

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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!

Similar Threads

  1. Stack implementation with given condition
    By Magesh in forum Member Introductions
    Replies: 2
    Last Post: August 31st, 2011, 01:17 AM
  2. Have Problem with if condition...Please Help me...
    By Jalpesh Ruparel in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 26th, 2011, 01:54 AM
  3. Waiting until condition
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: October 22nd, 2010, 09:24 AM
  4. Unable to retriving the data using WHERE condition
    By bhaskar_reddy in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: May 14th, 2010, 02:40 AM
  5. Problem with condition
    By shamed in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 7th, 2009, 04:51 PM