C# Programming - Operators - Discussion
Discussion Forum : Operators - General Questions (Q.No. 17)
17.
What will be the output of the C#.NET code snippet given below?
int a = 10, b = 20, c = 30;
int res = a < b ? a < c ? c : a : b;
Console.WriteLine(res);
Discussion:
15 comments Page 1 of 2.
Nikunj said:
1 decade ago
a < b ? a < c ? c : a : b;
a < b ? (a < c ? c : a) : b;
a < b ? (c) : b;
Now from above.we get.
c = 30;
So, 30 is right.
a < b ? (a < c ? c : a) : b;
a < b ? (c) : b;
Now from above.we get.
c = 30;
So, 30 is right.
(1)
Ahmed said:
10 years ago
a < b ? a < c ? c : a : b;
Make two separate condition,
a < b ? (a < c ? c : a) : b;
Value of a = 10, b= 20, c=30.
Step 1 :- a < b means (10 < 20) then Ans b = 20.
Now it will check for inner condition,
Step 2: - (a < c ? c : a) means (10 < 30) then Ans c = 30.
Now it will compare both inner and outer values,
Step 3 : - From outer we got b = 20 and from inner we got c =30.
(20 ? (30) c:b).
Now it will check which one is greater,
Step 4 : - here b < c means (20 < 30) then Ans c = 30.
Make two separate condition,
a < b ? (a < c ? c : a) : b;
Value of a = 10, b= 20, c=30.
Step 1 :- a < b means (10 < 20) then Ans b = 20.
Now it will check for inner condition,
Step 2: - (a < c ? c : a) means (10 < 30) then Ans c = 30.
Now it will compare both inner and outer values,
Step 3 : - From outer we got b = 20 and from inner we got c =30.
(20 ? (30) c:b).
Now it will check which one is greater,
Step 4 : - here b < c means (20 < 30) then Ans c = 30.
(1)
Divya said:
1 decade ago
Can anyone explain how the answer came?
Sarthak said:
1 decade ago
@divya:
((a<b)?(a<c?c:a):b))
first a<c ans is true so ans c=30....next a<b ?c:b ans is true again so c...n c=30.done..!!
((a<b)?(a<c?c:a):b))
first a<c ans is true so ans c=30....next a<b ?c:b ans is true again so c...n c=30.done..!!
Bhavika said:
1 decade ago
Please explain more briefly.
Prakash said:
1 decade ago
I didnt get it:- (.
Some one please explain properly.
Some one please explain properly.
Norbert said:
1 decade ago
The Syntax of Conditional Operator is:
(Condition)?True:False
Q: a < b ? a < c ? c : a : b;
A:
First you just separate the conditions from the given statement:
a < b ? (a < c ? c : a) : b
In the Above step i just separated the condition,
Now (a < c ? c : a) In this condition the answer is 30
(i.e) is c
Now the statement changes into
a < b ? c : b
Just replace that Statement with above answer.
And Now that answer is C..
Thats it..
I hope you may got it.. Thank you..
(Condition)?True:False
Q: a < b ? a < c ? c : a : b;
A:
First you just separate the conditions from the given statement:
a < b ? (a < c ? c : a) : b
In the Above step i just separated the condition,
Now (a < c ? c : a) In this condition the answer is 30
(i.e) is c
Now the statement changes into
a < b ? c : b
Just replace that Statement with above answer.
And Now that answer is C..
Thats it..
I hope you may got it.. Thank you..
Anant mishra said:
1 decade ago
Q a<b?a<c?c:a:b;
A 10<20?10<30?30:10:20;
10<20 the condition true.
Then,
A<c?c:a;
10<30?30:10;
Know again condition true.
Answer is c=30;
They are three argument.
Left side argument, middle side argument and right side argument.
We are working with conditional operator expression is true then return middle argument if expression false then return with side argument.
A 10<20?10<30?30:10:20;
10<20 the condition true.
Then,
A<c?c:a;
10<30?30:10;
Know again condition true.
Answer is c=30;
They are three argument.
Left side argument, middle side argument and right side argument.
We are working with conditional operator expression is true then return middle argument if expression false then return with side argument.
Chandru LS said:
1 decade ago
//If two conditions are true so, it prints c value(first expression)
int a = 10, b = 20, c = 30;
int res1 = a < b ? a < c ? c : a : b;
Console.WriteLine(res1);
Console.ReadLine();
//If first true and 2nd false , it prints a value(2nd expression)
int rqw = a<b? a>c? c:a:b;
Console.WriteLine(rqw);
Console.ReadLine();
//If first false and 2nd true , it prints b value(3rd expression)
int ras = a > b ? a < c ? c : a : b;
Console.WriteLine(ras);
Console.ReadLine();
// If both false it prints b value(3rd exp)
int bmw = a > b ? a > c ? c : a : b;
Console.WriteLine(bmw);
Console.ReadLine();
o/p: 30 press enter
10
20
20
int a = 10, b = 20, c = 30;
int res1 = a < b ? a < c ? c : a : b;
Console.WriteLine(res1);
Console.ReadLine();
//If first true and 2nd false , it prints a value(2nd expression)
int rqw = a<b? a>c? c:a:b;
Console.WriteLine(rqw);
Console.ReadLine();
//If first false and 2nd true , it prints b value(3rd expression)
int ras = a > b ? a < c ? c : a : b;
Console.WriteLine(ras);
Console.ReadLine();
// If both false it prints b value(3rd exp)
int bmw = a > b ? a > c ? c : a : b;
Console.WriteLine(bmw);
Console.ReadLine();
o/p: 30 press enter
10
20
20
Bhagya said:
1 decade ago
a<b?a<c?c:a:b
Now a<b?(a<c?c:a):b.
Since a is less than c so answer is c because of conditional operator is used
a<b?c:b.
Similarly here b is greater so we have c value as output.
Now a<b?(a<c?c:a):b.
Since a is less than c so answer is c because of conditional operator is used
a<b?c:b.
Similarly here b is greater so we have c value as output.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers