Thread: conditional operator, 3 conditions

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    3

    conditional operator, 3 conditions

    Hello Everyone,
    I am newbie in C language and don't have good knowledge about C.
    i want anyone to explain me conditional operators and how to implement it for 3 conditions ?

    for example if i want to make a program that print the maximum value from the 3 input values how can i do this ??
    i've done this for two values i.e
    Code:
    d=(a>b)? a:b;
    but don't know how to do it for 3 values.
    can anyone please explain conditional operators ? and how to use it

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    d=(a>b)? a:b;
    is the same as
    Code:
    if(a>b)
       d = a;
    else
       d = b;
    to find max of 3 numbers - find max of 1st and second and compare it to 3rd...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    but i don't know how to apply more than two conditions in Conditional operators..i am a student and we've studied only applying 2 conditions.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by EffecTed View Post
    but i don't know how to apply more than two conditions in Conditional operators..i am a student and we've studied only applying 2 conditions.
    A conditional operator can only test one condition. If you need more conditions, then you need more operators. You can nest ?: just like you nest if statements inside each other, although (1) I don't know why you would want to and (b) be careful with your parentheses, if you do.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    hm thanks alot, that is what i was looking for..thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple conditions for while
    By sfff in forum C Programming
    Replies: 5
    Last Post: 11-08-2009, 11:10 PM
  2. "C4127: conditional expression is constant" Huh?
    By cpjust in forum C++ Programming
    Replies: 27
    Last Post: 03-13-2008, 04:58 PM
  3. regarding conditional statement in xml
    By cnu_sree in forum C Programming
    Replies: 5
    Last Post: 07-21-2007, 10:22 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM