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.
java decision statements
Collapse
X
-
Two vertical bars '||'.Originally posted by UMstudentIts 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.
Adrian -
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.Originally posted by r035198xif((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.
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.Code:int a = 1; int b = 2; if (a & b) { // output true } else { // output false }
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.
AdrianComment
-
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?Originally posted by AdrianHSorry, 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.
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.Code:int a = 1; int b = 2; if (a & b) { // output true } else { // output false }
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.
AdrianComment
-
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.Originally posted by r035198xThanks 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?
AdrianComment
Comment