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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: && , || usage?

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Question && , || 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;


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default 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".

  3. #3
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: && , || usage?

    Quote Originally Posted by hemla View Post
    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 View Post
    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.

Similar Threads

  1. Eclipse is always locked [High CPU & RAM usage]
    By talha06 in forum Java IDEs
    Replies: 4
    Last Post: March 16th, 2010, 10:07 AM