If we write a simple program of finding maximum of two numbers, using if-else, we can write
The above program can be replaced by a conditional expression (a > b)? a: b; . The general form of a conditional expression isExpression1? Expression2: Expression3;
Here Expression1 is evaluated first. If it is true then Expression2 is evaluated and that is the value of the expression. If it is false, Expression3 is evaluated and gives value of conditional expression. Thus in our example, if a is greater than b then value of expression is a, else value of expression is b. Conditional expressions can appear on right hand side of a simple assignment. The resulting value is assigned to the variable on the left.
Apart from arithmetic statements, conditional operators can be used in other places as well.
Conditional operators can be nested
If operands (i.e. Expression2 and Expression3) differ in type, then the resulting data type of conditional expression will be determined by conversion rules stated in previous chapter.
If f is a float and n is an integer then the result is of type float. The limitation of conditional operators is that only one statement can appear after ‘?’ or after ‘.’.
C Related Interview Questions
|DBMS Interview Questions||C++ Interview Questions|
|C & Data Structures Interview Questions||Java Interview Questions|
|Go (programming language) Interview Questions||F Sharp (programming language) Interview Questions|
|C preprocessor Interview Questions||R Programming language Interview Questions|
|D Programming Language Interview Questions||Lua (programming language) Interview Questions|
|Embedded C Interview Questions|
Introduction To C-programming
Data Types And Storage Classes
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.