java decision statements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • UMstudent
    New Member
    • Feb 2007
    • 5

    #1

    java decision statements

    Its a simple problem really but I can't remember for the life of me the code for "or" I know && is used for and. Basically I have to write a program that tells accepts side lengths of a triangle and then tells what type of triangle it is. So for example (num1 = num2 or num1 = num3); whats the code for or. Thanks.
  • AdrianH
    Recognized Expert Top Contributor
    • Feb 2007
    • 1251

    #2
    Originally posted by UMstudent
    Its a simple problem really but I can't remember for the life of me the code for "or" I know && is used for and. Basically I have to write a program that tells accepts side lengths of a triangle and then tells what type of triangle it is. So for example (num1 = num2 or num1 = num3); whats the code for or. Thanks.
    Two vertical bars '||'.

    Adrian

    Comment

    • AdrianH
      Recognized Expert Top Contributor
      • Feb 2007
      • 1251

      #3
      Originally posted by r035198x
      if((num1 == num2) || (num1 == num3))

      The single bar is the short circuit

      if((num1 == num2) | (num1 == num3))

      If num1 == num2 then the compiler does not go on to check whether num1 == num3 or not. The result of the whole expression would still be true anyway. Short circuit is more efficient of course but should be used with a lot of care.
      Sorry, but you are incorrect. Java is based on C/C++ in many ways and this is one of them. A single | is a bitwise or and is evaluated no matter what, || is a logical or and is short circuited. Using a | as a logical operator on non-Boolean operands will work proably as you expect (assuming 0 is false and non-zero is true), but using a & as a logical operator on non-Boolean operands can cause unexpected results.
      Code:
      int a = 1;
      int b = 2;
      if (a & b) {
        // output true
      }
      else {
        // output false
      }
      This pseudo-code will output false not true. To prevent this, ensure that your operands for your bitwise operators are Boolean when using them as non-short-circuiting conditionals.

      See this artical for more information.

      Short-circuiting conditionals are the norm as it keeps unnecessary code from being run. If you are using non-short-circuiting conditionals, you are an advanced programmer and have some specific reason as to why you want to have the entire expression evaluated when it is unnecessary to do so.


      Adrian

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by AdrianH
        Sorry, but you are incorrect. Java is based on C/C++ in many ways and this is one of them. A single | is a bitwise or and is evaluated no matter what, || is a logical or and is short circuited. Using a | as a logical operator on non-Boolean operands will work proably as you expect (assuming 0 is false and non-zero is true), but using a & as a logical operator on non-Boolean operands can cause unexpected results.
        Code:
        int a = 1;
        int b = 2;
        if (a & b) {
        // output true
        }
        else {
        // output false
        }
        This pseudo-code will output false not true. To prevent this, ensure that your operands for your bitwise operators are Boolean when using them as non-short-circuiting conditionals.

        See this artical for more information.

        Short-circuiting conditionals are the norm as it keeps unnecessary code from being run. If you are using non-short-circuiting conditionals, you are an advanced programmer and have some specific reason as to why you want to have the entire expression evaluated when it is unnecessary to do so.


        Adrian
        Thanks AdrianH. I seem to have interchanged my operands there. I got them right in post number 2 of the java classes. What I do not understand is what you mean by "ensure that your operands for your bitwise operators are Boolean when using them as non-short-circuiting conditionals.". Are you saying that it's possible to use these operators on non boolean operands?

        Comment

        • AdrianH
          Recognized Expert Top Contributor
          • Feb 2007
          • 1251

          #5
          Originally posted by r035198x
          Thanks AdrianH. I seem to have interchanged my operands there. I got them right in post number 2 of the java classes. What I do not understand is what you mean by "ensure that your operands for your bitwise operators are Boolean when using them as non-short-circuiting conditionals.". Are you saying that it's possible to use these operators on non boolean operands?
          Yes, bitwise referrs to the fact that they do a bit by bit comparison with the operands. With a Boolean, there is only one bit, with an int there are 32 bits. See the link in my last post for more info.

          Adrian

          Comment

          Working...