Results 1 to 7 of 7
  1. #1
    Ranu is offline Member
    Join Date
    Jun 2010
    Posts
    13
    Rep Power
    0

    Default if block with logical or

    I want to know whether it is possible to to know the particular case for which the if block is true?Like
    if(a>b || b>c || c>d)
    {
    then which case is true?
    }
    a,b,c,d are arbitrary numbers.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    No, that is not possible, without evaluating very expression on it's own.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    arun9683 is offline Member
    Join Date
    Jun 2010
    Location
    Bangalore,India
    Posts
    70
    Rep Power
    0

    Default

    No its not possible to identify which condition became true.

    The work around is have nested if blocks

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Ranu View Post
    I want to know whether it is possible to to know the particular case for which the if block is true?Like
    if(a>b || b>c || c>d)
    {
    then which case is true?
    }
    a,b,c,d are arbitrary numbers.
    It's a bit ugly but you could do this:

    Java Code:
    boolean alb= false; // a > b condition
    boolean blc= false; // b > c condition
    boolean cld= false; // c > d condition
    
    if|((alb= a > b) || (blc= b > c) || (cld= c > d) 
       // use alb and blc and cld to check which is true
    Note that if, say, blc was true cld doesn't reflect the truth value of c > d.

    kind regards,

    Jos

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    That if statement you put up essentially says "if any of these are true then...".

    If you need to know which of them is true then presumably you need to know that in order to do some processing, so you would need an "if...else if...else".

    ETA: Just realised the "if...else if...else" has a similar problem to Jos's in that it will only handle the first it encounters. But anyway, you get the idea hopefully.

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Just don't use short circuiting and Jos' approach:
    Java Code:
    if((alb= a > b) | (blc= b > c) | (cld= c > d))
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  7. #7
    Ranu is offline Member
    Join Date
    Jun 2010
    Posts
    13
    Rep Power
    0

Similar Threads

  1. Some Logical problem
    By MuslimCoder in forum New To Java
    Replies: 4
    Last Post: 03-01-2010, 08:12 AM
  2. Logical Gates
    By lingz89 in forum New To Java
    Replies: 1
    Last Post: 08-17-2009, 01:11 AM
  3. Logical JDBC problem
    By nick2price in forum Advanced Java
    Replies: 3
    Last Post: 10-02-2008, 11:34 PM
  4. Listing Logical Drives
    By Juggler in forum New To Java
    Replies: 3
    Last Post: 08-10-2008, 07:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •