## Xufyan -4

Hello using conditional operators I want to make a program that ask user to input three values and print the maximum among those three values.
i have done for two values but dont know the logic for three values.

``````int main(void)
{
int a,b,c,d;
printf ("first value");
scanf ("%d",&a);
printf ("second value");
scanf ("%d",&b);
printf ("third value");
scanf ("%d",&c);
d=(a>b && a>c)? a:b;
printf ("%d",d);``````

how can i write a code for three conditions ?

## Narue 5,707

>how can i write a code for three conditions ?
I assume you're looking for the ugly nesting solution:

``int d = a < b ? a < c ? a : c : b < c ? b : c;``

Which looks a lot like line noise and should be avoided in well written code. A feasible alternative is using a macro to hide the ugliness:

``````#define min(a, b) ((a) < (b) ? (a) : (b))

int d = min ( a, min ( b, c ) );``````

## Xufyan -4

hey i've coded this with the help of you, it is working fine but i don't know why :)....why it is printing the Maximum value among those three input values ?

``````int main(void)
{
int a,b,c,d;
printf ("Enter first value");
scanf ("%d",&b);
printf ("Enter second value");
scanf ("%d",&a);
printf ("Enter third value");
scanf ("%d",&c);
d=a>b? b>c? a:b:c;
printf ("The maximum value is %d",d);
}``````

## Narue 5,707

>why it is printing the Maximum value among those three input values ?
It isn't. If b is greater than or equal to a, but c is less than b, your code will erroneously print c rather than b. It helps if you write the equivalent if statement to see the logic flow:

``````/* d=a>b? b>c? a:b:c */
if ( a > b ) {
if ( b > c )
d = a;
else
d = b;
}
else {
d = c;
}``````
commented: Ty.:)..Xufyan +0

## Xufyan -4

how can you use two else conditions ??
.
.
can anyone please compare these two conditions and explain them

1- `d=(a>b && a>c)? a:b;` 2- `d=a>b? b>c? a:b:c;` in second condition when i am using `()` then why it is not working ??
and please explain this conditional operators

## Xufyan -4

My program is still n0t w0rking :s
i searched how to use three conditions in conditional operator but didn't find any where,
you've explained using if else but how to implement it in conditional operator.

## Narue 5,707

>i searched how to use three conditions in conditional operator but didn't find any where
Then I guess you didn't look in this thread. My first reply gives you this:

``int d = a < b ? a < c ? a : c : b < c ? b : c;``

My previous reply showed you a conversion to if statements from which you can derive the equivalent code:

``````int d;

/* d = a < b ? a < c ? a : c : b < c ? b : c */
if ( a < b ) {
if ( a < c )
d = a;
else
d = c;
}
else {
if ( b < c )
d = b;
else
d = c;
}``````

## jephthah 1,888

Xufan your codes don't work because they are wrong. Narue has given you code that works.

now do the following:

(1) read Narues post above and fully understand the second example with the if/else statements.

(2) take the first statement with teh ? : operators, and insert parentheses around them so that:
(a) the order of operations remain the same, and
(b) it is easier for you to follow the logic

you should get no more help here than this. we're not here to type your homework up and submit it for you.

.

## Xufyan -4

i am not asking for the solution or a home work ,
i've already coded my program sir,
actually, i just wanted someone to explain me this

``d = (a > b)? (a > c)? a : c : (b > c)? b : c;``

because i don't know how to use more than two conditions in a conditional operator,
we've learned to use to conditions i.e

``d=(a>b?) a:b;``

but did't know about more than two conditions ?

in the above code can you explain how can you write `(b > c)? b` after `:` ??

## Xufyan -4

thanks alot everyone, finally i' got my answer

## jephthah 1,888

``````1-- if (a > b)
2--    and if (a > c)
3--       then "a"
4--    otherwise
5--       then "c"

6-- else if (b > a)
7--    and if (b > c)
8--       then "b"
9--    otherwise
10-       then "c"``````

now see this:

``````(a > b) ? ( (a > c) ? a : c) ) : _______________________

---1---   ( ---2---  -3- -4- )          ( --?-- )
if     ( and if  then else)          ( else  )``````

## jephthah 1,888

OOPS.... ^ thats wrong ^

Line #2 should read `2-- and if (b > c)` note also, that the "else if" pseudo-code in line i have numbered as #6 is implied, would not be coded, and actually will result in an "else" statement that logically meets the condition `b >= a` , so this conditional method doesnt note the condition when two values are tied for the largest value.

other than that, the following should be correct:

``````(a > b) ? ( (a > c) ? a : c  ) : ( (b > c) ? b : c )

---1---   ( ---2---  -3- -5- )    ---------6---------
---7---  -8- -10-
if      ( and if  then else )  (       else         )
(    if   then else  )``````
commented: thanks alot . xufyan +0