C Programming - Expressions - Discussion
Discussion Forum : Expressions - General Questions (Q.No. 3)
3.
Which of the following is the correct usage of conditional operators used in C?
Answer: Option
Explanation:
Option A: assignment statements are always return in paranthesis in the case of conditional operator. It should be a>b? (c=30):(c=40);
Option B: it is syntatically wrong.
Option D: syntatically wrong, it should be return(a>b ? a:b);
Option C: it uses nested conditional operator, this is logic for finding greatest number out of three numbers.
Discussion:
30 comments Page 1 of 3.
Souravb said:
2 decades ago
I cant understand it. Please help.
Afsheen said:
2 decades ago
If b>c then b is true else c is true, so 1st b>c is evaluated and stored then a>c is evaluated and stored. now the whole expression is considered.
Let us assume
(i) in b>c case this stmnt is true so b is saved.
(ii)in a>c case this stmnt is false so c is saved.
now the expression is in the form
max=a>b?c:b
Now finally this is evaluated.
you just assume the given stmnt as a>b?(a>c?a:c):(b>c?b:c).
I hope you understood.
Let us assume
(i) in b>c case this stmnt is true so b is saved.
(ii)in a>c case this stmnt is false so c is saved.
now the expression is in the form
max=a>b?c:b
Now finally this is evaluated.
you just assume the given stmnt as a>b?(a>c?a:c):(b>c?b:c).
I hope you understood.
Durga said:
1 decade ago
I am not under stand please clear about it. In detailed because I don't know how to use ? opearator. So plese clear with a example how to use this operator.
Ramu said:
1 decade ago
Simply we have to think in general if..else format
like if(a>b)
{
if(a>c)
max is a
else
max is c
}
else if(b>c)
max is b
else
max is c
This is implemented , thinking in this we could easily analyse for greatest of 3 or 4 or 5 and so on easily I think so.
like if(a>b)
{
if(a>c)
max is a
else
max is c
}
else if(b>c)
max is b
else
max is c
This is implemented , thinking in this we could easily analyse for greatest of 3 or 4 or 5 and so on easily I think so.
Vishwas Pathak said:
1 decade ago
Option A is also right i did it in the dev-c++ and i got first one as the answer.
Vinutha said:
1 decade ago
Can any one tell the correct syntax of all operators?
Heena said:
1 decade ago
max = a>b ? (a>C ? a : c) : (b>c ? b :c);
Now I guess this statement is more clear.
Now I guess this statement is more clear.
(2)
Shashank said:
1 decade ago
What if in 'max = a>b ? (a>C ? a : c) : (b>c ? b :c)'
c is greater than both a and b, then the equation will be
max = a>b?c:c
Then how would the statement be evaluated? though the answer is C, I want the steps compiler takes to evaluate it.
c is greater than both a and b, then the equation will be
max = a>b?c:c
Then how would the statement be evaluated? though the answer is C, I want the steps compiler takes to evaluate it.
DEVIPRIYA said:
1 decade ago
Max = a>b?(a>c?a:c):(b<c?b:c).
First it checks a>b if condition is true then Max = (a>c?a:c).
Else Max = (b<c?b:c).
Again it checks the condition like this.
First it checks a>b if condition is true then Max = (a>c?a:c).
Else Max = (b<c?b:c).
Again it checks the condition like this.
Azhar said:
1 decade ago
It is not terminated with ";" (option C).
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers