473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.
Feb 9 '07 #1
4 1552
AdrianH
1,251 Expert 1GB
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
Feb 9 '07 #2
AdrianH
1,251 Expert 1GB
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.
Expand|Select|Wrap|Line Numbers
  1. int a = 1;
  2. int b = 2;
  3. if (a & b) {
  4.   // output true
  5. }
  6. else {
  7.   // output false
  8. }
  9.  
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
Feb 12 '07 #3
r035198x
13,262 8TB
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.
Expand|Select|Wrap|Line Numbers
  1. int a = 1;
  2. int b = 2;
  3. if (a & b) {
  4. // output true
  5. }
  6. else {
  7. // output false
  8. }
  9.  
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?
Feb 12 '07 #4
AdrianH
1,251 Expert 1GB
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
Feb 12 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

198
by: Michael N. Christoff | last post by:
Java, the software developed by Sun Microsystems in the mid-1990s as a universal operating system for Internet applications, gave NASA a low-cost and easy-to-use option for running Spirit, the...
73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
114
by: Maurice LING | last post by:
This may be a dumb thing to ask, but besides the penalty for dynamic typing, is there any other real reasons that Python is slower than Java? maurice
37
by: asj | last post by:
awhile back, eBay decided to switch from a Microsoft/.NET/Windows architecture on the backend to a J2EE one, which might explain why their java backend will handle up to 1 BILLION page views a day!...
30
by: Rhino | last post by:
I am giving some thought to applying for some jobs that want people with Java and C++ experience. I have been writing Java for several years and am fluent enough that I don't have to get help with...
2
by: Rafael Faria | last post by:
Hi All, We are starting a large data warehousing project using DB2 8.2 on AIX. There is a direction to move any new internal development to Java and a question was raised: Would it be a good...
458
by: wellstone9912 | last post by:
Java programmers seem to always be whining about how confusing and overly complex C++ appears to them. I would like to introduce an explanation for this. Is it possible that Java programmers...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
5
by: r035198x | last post by:
Setting up. Getting started To get started with java, one must download and install a version of Sun's JDK (Java Development Kit). The newest release at the time of writting this article is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.