Thread: Difference between two if conditions ?...

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    5

    Difference between two if conditions ?...

    I am trying to express a condition of a problem: "among three non-zero integers, if any one integer is greater than or equal to the sum of the the other two integers" as if condition:

    Code:
    if ( (a || b ||c) >= (a+b || b+c || c+a) )
    which is giving the wrong output and the correct condition expression is:

    Code:
    if (a >= b+c  ||  b>=c+a  ||  c>=a+b )
    Can anybody please explain the problem with the first condition.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    && and || evaluate to either 1 or 0 (they're booleans)

    So the result of a||b||c is either 1 or 0.

    So what you have is for this
    > if ( (a || b ||c) >= (a+b || b+c || c+a) )
    is
    if ( (something_evaluating_to_1or0) >= (something_evaluating_to_1or0) )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin with conditions
    By sean_cantab in forum C++ Programming
    Replies: 5
    Last Post: 09-24-2016, 11:31 PM
  2. if conditions
    By RyanC in forum C Programming
    Replies: 4
    Last Post: 08-12-2015, 06:29 AM
  3. if statement with 2 conditions
    By robando in forum C Programming
    Replies: 2
    Last Post: 04-12-2015, 04:18 AM
  4. Some questions about conditions
    By student111 in forum C++ Programming
    Replies: 2
    Last Post: 01-08-2012, 02:47 AM
  5. if and while conditions
    By firehydrant in forum C Programming
    Replies: 12
    Last Post: 03-05-2011, 03:07 AM

Tags for this Thread