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 5 of 5

Thread: Conditionals with more than one condition.

  1. #1
    Member
    Join Date
    May 2013
    Posts
    106
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Conditionals with more than one condition.

    Okay this is kind of an elementary question that I feel like I should know the answer to. I'm trying to debug a database program and I think there's something I don't understand about if statements with multiple conditions separated by OR, and that's screwing up my code. The OR has screwed me up many times (usually because I use it in situations where AND is needed).

    Not gonna go into detail with the code, so I'm just going to present some generic pseudo-code.

    In the statement:

    if( (condition1) || (condition2) )
      --statements executed under true conditions
    else
     --statements executed under false conditions.

    How is that if statement evaluated? Suppose condition1 is true and condition2 is false. Does it say "Okay, condition1 is true, let's check condition2. Okay, now I see condition2 is false. Let's jump to the body of the else statement."? Or does it automatically say "Okay, condition1 is true, let's jump to the body of the if statement."

    Let's reverse the situation. Suppose condition1 is false and condition2 is true. Does it say "Okay, condition1 is false. Let's check condition2. Okay condition2 is true, so let's jump to the body of the if statement."? Or does it automatically jump straight to that else after it sees that condition1 is false?

    Bottom line, is there a universe in which that overall statement resolves as true if just one of the conditions is true but any others are false? Is there an order of operations at play here?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Conditionals with more than one condition.

    In order for the body of the if statement to execute, the two conditions OR'd together must be true. For OR, either statement being true will result in the result true:

    OR truth table:
    true || true = true
    true || false = true
    false || true = true
    false || false = false

    Let's consider first the AND combination in which both expressions have to be true for the combination to be true. Java continues to evaluate the entire conditional expression only while the outcome is uncertain. In a ( false && true ) situation, Java will not check the 'true' expression once the 'false' is determined.

    Similarly for the ( true || don't-care ) combination, once the true is realized, the evaluation will stop.

    Yes, I believe the universe you describe exists in a combination of OR'd statements. You can learn more about operator precedence here.

  3. #3
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Conditionals with more than one condition.

    when you are using && both statements have to be true...when you are using || only 1 of the statements has to be true which ever comes first

    for || it only reads the conditions till it receives a true condition so if your first one is true it wont read the other conditions

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Conditionals with more than one condition.

    Quote Originally Posted by mstabosz View Post
    Bottom line, is there a universe in which that overall statement resolves as true if just one of the conditions is true but any others are false? Is there an order of operations at play here?
    Bottom line, Yes, all of them.
    Read this || as "either or" to make the sentence clearer in your mind. "if either c1 OR c2 is true, the whole statement is true"
    Read this && as "both and" to make the sentence clearer in your mind. "if both c1 AND c2 are true, the whole statement is true"

  5. #5
    Member
    Join Date
    May 2013
    Posts
    106
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: Conditionals with more than one condition.

    Thanks all. It is as I suspected. And actually right after I made this thread I wrote up this short little test program which fleshed it all out. It prints out 1--TRUE 2--TRUE. I think that my problem with the original program isn't that it isn't going down the right road, but where it goes at the end of that road. There's a lot of nested if-else blocks and I think I may have left out a bracket somewhere so it's executing extra statements that I don't want it to. Having a devil of a time tracing it and think I might just have to print out a hard copy of the code just to double check all the placements of ifs, elses, and brackets.

     
    public class scratchpad 
    {
    	public static void main(String[] args) 
    	{
    		if((true) || (false))
    			System.out.println("1--TRUE");
    		else
    			System.out.println("1--FALSE");
     
    		if((false) || (true))
    			System.out.println("2--TRUE");
    		else
    			System.out.println("2--FALSE");
    	}
     
    }

Similar Threads

  1. condition number? what is it?
    By gspease839 in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 9th, 2013, 10:00 PM
  2. condition number help?
    By gspease839 in forum Algorithms & Recursion
    Replies: 4
    Last Post: February 9th, 2013, 07:40 PM
  3. Loops and conditionals
    By chazzkat in forum Loops & Control Statements
    Replies: 2
    Last Post: February 18th, 2012, 10:27 AM
  4. Have Problem with if condition...Please Help me...
    By Jalpesh Ruparel in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 26th, 2011, 01:54 AM
  5. Problem with condition
    By shamed in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 7th, 2009, 04:51 PM