0 like 0 dislike
39 views
in Computer Science by (1.0m points)
Briefly explain conditional operator and give example.

1 Answer

0 like 0 dislike
by (1.0m points)
The conditional operator (? :) is a ternary operator (it takes three operands). The conditional operator works as follows:

The first operand is implicitly converted to bool. It is evaluated and all side effects are completed before continuing.

If the first operand evaluates to true (1), the second operand is evaluated.

If the first operand evaluates to false (0), the third operand is evaluated.

The result of the conditional operator is the result of whichever operand is evaluated — the second or the third. Only one of the last two operands is evaluated in a conditional expression.

Conditional expressions have right-to-left associativity. The first operand must be of integral or pointer type. The following rules apply to the second and third operands:

If both operands are of the same type, the result is of that type.

If both operands are of arithmetic or enumeration types, the usual arithmetic conversions (covered in Standard Conversions) are performed to convert them to a common type.

If both operands are of pointer types or if one is a pointer type and the other is a constant expression that evaluates to 0, pointer conversions are performed to convert them to a common type.

If both operands are of reference types, reference conversions are performed to convert them to a common type.

If both operands are of type void, the common type is type void.

If both operands are of the same user-defined type, the common type is that type.

If the operands have different types and at least one of the operands has user-defined type then the language rules are used to determine the common type. (See warning below.)

Any combinations of second and third operands not in the preceding list are illegal. The type of the result is the common type, and it is an l-value if both the second and third operands are of the same type and both are l-values.

Related questions

0 like 0 dislike
0 answers 46 views
0 like 0 dislike
1 answer 155 views
asked Feb 9, 2019 in Biology by danish (1.0m points)
0 like 0 dislike
0 answers 82 views
0 like 0 dislike
1 answer 57 views
0 like 0 dislike
1 answer 34 views
0 like 0 dislike
0 answers 55 views
0 like 0 dislike
0 answers 52 views
0 like 0 dislike
1 answer 56 views
Welcome to Free Homework Help, where you can ask questions and receive answers from other members of the community. Anybody can ask a question. Anybody can answer. The best answers are voted up and rise to the top. Join them; it only takes a minute: School, College, University, Academy Free Homework Help

19.4k questions

18.3k answers

8.7k comments

6.2k users

Free Hit Counters
...