C# Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - General Questions (Q.No. 16)
16.

Which of the following is another way to rewrite the code snippet given below?

int a = 1, b = 2, c = 0;
if (a < b) c = a;
int a = 1, b = 2, c = 0;
c = a < b ? a : 0;
int a = 1, b = 2, c = 0;
a < b ? c = a : c = 0;
int a = 1, b = 2, c = 0;
a < b ? c = a : c = 0 ? 0 : 0;
int a = 1, b = 2, c = 0;
a < b ? return (c): return (0);
int a = 1, b = 2,c = 0;
c = a < b : a ? 0;
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
6 comments Page 1 of 1.

Warunika Senadheera said:   1 decade ago
Can anyone explain the answer?

Wiinai said:   10 years ago
Condition operator used in this code.

a<b condition true then it return 'a' value.

If condition false then it return 0;

Then that return value equals to C.

C = (a<b) ? a:0;

(or) If (a<b)c = a;

Snehal said:   9 years ago
Why not option B?

Madusha said:   6 years ago
But is there anything wrong with the ternary operator in the option B?

Afsha said:   5 years ago
I am confused with option A and B. Which one is right and why? Please explain.

Sparwhawk said:   4 years ago
B is incorrect because ternary operation cannot be written as a statement. In the case of answer A, the ternary operation is assigned to a variable C.

Post your comments here:

Your comments will be displayed after verification.