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

# Thread: && , || usage?

1. ## && , || usage?

Hello, i'd like to confirm the following:

suppose the question is: we have three int's (int a, int b, int c) and the value is true if and only if the sum of a and c is greater than b, then b is divisible by 3 without remainder.
Do both the answers below mean the same?
```if (!((a + c) > b) || b % 3 == 0) {
return true;```

```if(a+ c>b &&  b%3==0){
return true;```  Reply With Quote

3. ## Re: && , || usage?

The way to test whether the two expressions are equivalent (rather than just think about them) is to print their values for lots of different input values of a, b and c.

---

Do you mean "the value is true if and only if if the sum of a and c is greater than b, then b is divisible by 3 without remainder"? If so, the first expression captures this meaning. The second means something like "the value is true if and only if both the sum of a and c is greater than b, and b is divisible by 3 without remainder".  Reply With Quote

4. ## Re: && , || usage? Originally Posted by hemla ```if (!((a + c) > b) || b % 3 == 0) {
return true;```
This if-statement will return true if a + c > b is false OR b is divisible by 3 without a remainder. The only time it will not return true is if a + c > b is true and b is not divisible by 3. Originally Posted by hemla ```if(a+ c>b &&  b%3==0){
return true;```
This if-statement returns true if a + c > b is true AND b is divisible by 3 without a remainder. This will return false if either a + c > b is false or b % 3 == 0 is false.

In Java, as well as other OOP languages, server-side languages (i.e. PHP) and client-side languages (i.e. JavaScript), && is the logical operator AND, while || is the logical operator OR.  Reply With Quote