# && , || usage?

Printable View

• April 22nd, 2013, 07:50 PM
hemla
&& , || 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?
Code Java:

```if (!((a + c) > b) || b % 3 == 0) { return true;```

Code Java:

```if(a+ c>b && b%3==0){ return true;```
• April 22nd, 2013, 08:04 PM
pbrockway2
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".
• April 23rd, 2013, 10:53 AM
SunshineInABag
Re: && , || usage?
Quote:

Originally Posted by hemla
Code Java:

```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.

Quote:

Originally Posted by hemla
Code Java:

```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.