answersLogoWhite

0


Best Answer

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.
User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

exp1 ? expr2 : exp3

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is conditional expression operator?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the conditional operators in c language?

The conditional operator is also known as ternary operator. It is called ternary operator because it takes three arguments. The conditional operator evaluates an expression returning a value if that expression is true and different one if the expression is evaluated as false.Syntax:condition ? result1 : result2If the condition is true, result1 is returned else result2 is returned.


Can a conditional operator replace an if statement always?

No. An if statement does not require an elseclause and the expression(s) do not return anything to the caller, whereas the conditional operator always executes one of two expressions and always returns the value of the executed expression back to the caller. The executed expression may be yet another conditional operator, thus allowing simulation of nested ifs and if...else if... else statements.Consider the following example:int x = rand();if( x > 100 ) x = 100;We can achieve the same result with a conditional operator:int y = rand();y = y>100 ? 100 : y;However, if we were to expand this statement to an if statement we would not get the original if statement shown above:int z = rand();if( z > 100 ) z = 100;else z = z;The else clause is clearly unnecessary in this case, so the original if statement would be the statement of choice here.As a general rule, if you can make use of the return value in a conditional operator, and must return one of at least two values, then use the conditional operator. Otherwise use an if statement.


How do you use the conditional statement of turbo c?

#include<stdio.h> void main() { int a=10,b=15; clrscr(); if(a>b) printf("%d is the large number",a); else printf("%d is the large number",b); getch(); }


Which operator is called ternary operator?

A ternary operator is an operator that requires three operands, as opposed to a binary operator that requires two operands and a unary operator that requires just one operand. C++ has just one ternary operator, the conditional ternary operator: <boolean expression> ? <expression #1> : <expression #2>; If the boolean expression evaluates true, the first expression is evaluated, otherwise the second expression is evaluated. A typical usage of this operator is to return the larger (or smaller) of two values of type T: template<typename T> T max (T a, T b) {return a<b ? b : a}; template<typename T> T min (T a, T b) {return a<b ? a : b}; These are really nothing more than notational shorthand for the following: template<typename T> T max (T a, T b) {if (a<b) return b; else return a; }; template<typename T> T min (T a, T b) {if (a<b) return a; else return b;}; However, because ternary expressions are evaluated, the return value of the expression can be used in more complex expressions: int a=42, b=0; // ... int c = ((a>b ? a : b) = 1); In the above expression, whichever is the larger of a and b will be assigned the value 1 which will also be assigned to c. Thus a and c become 1 while b remains 0.


Can you use conditional operator in cout statement?

Yes, but 'cout' is not a statement! Examples for statements: null-statement, block, expression, if-else, while, do-while, for, continue, switch, break, return.

Related questions

What is the conditional operators in c language?

The conditional operator is also known as ternary operator. It is called ternary operator because it takes three arguments. The conditional operator evaluates an expression returning a value if that expression is true and different one if the expression is evaluated as false.Syntax:condition ? result1 : result2If the condition is true, result1 is returned else result2 is returned.


What are conditional operators in C language?

The conditional operator in C (and C++, C# and other languages) consists of two symbols, '?' and ':'. Together, they can be used to form an expression from three subexpressions:e1 ? e2 : e3The conditional operator is evaluated in two steps; first, the expression e1 is evaluated, if it has a true value, then e2 is evaluated and its value is returned as the result of the entire expression, otherwise (if e1 is false) e3 is evaluated and its value is returned as the result of the entire expression.


What Java conditional expression correctly represents the mathematical expression 10 x 100?

There is no need for a conditional expression; just write it as 10 * 100.


Can a conditional operator replace an if statement always?

No. An if statement does not require an elseclause and the expression(s) do not return anything to the caller, whereas the conditional operator always executes one of two expressions and always returns the value of the executed expression back to the caller. The executed expression may be yet another conditional operator, thus allowing simulation of nested ifs and if...else if... else statements.Consider the following example:int x = rand();if( x > 100 ) x = 100;We can achieve the same result with a conditional operator:int y = rand();y = y>100 ? 100 : y;However, if we were to expand this statement to an if statement we would not get the original if statement shown above:int z = rand();if( z > 100 ) z = 100;else z = z;The else clause is clearly unnecessary in this case, so the original if statement would be the statement of choice here.As a general rule, if you can make use of the return value in a conditional operator, and must return one of at least two values, then use the conditional operator. Otherwise use an if statement.


Which is a valid conditional formatting operator found in the New Formatting Rule dialog box?

Equal Sign (=) is a valid conditional formatting operator found in the New Formatting Rule dialog box.Equal Sign (=) is a valid conditional formatting operator found in the New Formatting Rule dialog box.


How do you use the conditional statement of turbo c?

#include<stdio.h> void main() { int a=10,b=15; clrscr(); if(a>b) printf("%d is the large number",a); else printf("%d is the large number",b); getch(); }


Which operator is called ternary operator?

A ternary operator is an operator that requires three operands, as opposed to a binary operator that requires two operands and a unary operator that requires just one operand. C++ has just one ternary operator, the conditional ternary operator: <boolean expression> ? <expression #1> : <expression #2>; If the boolean expression evaluates true, the first expression is evaluated, otherwise the second expression is evaluated. A typical usage of this operator is to return the larger (or smaller) of two values of type T: template<typename T> T max (T a, T b) {return a<b ? b : a}; template<typename T> T min (T a, T b) {return a<b ? a : b}; These are really nothing more than notational shorthand for the following: template<typename T> T max (T a, T b) {if (a<b) return b; else return a; }; template<typename T> T min (T a, T b) {if (a<b) return a; else return b;}; However, because ternary expressions are evaluated, the return value of the expression can be used in more complex expressions: int a=42, b=0; // ... int c = ((a>b ? a : b) = 1); In the above expression, whichever is the larger of a and b will be assigned the value 1 which will also be assigned to c. Thus a and c become 1 while b remains 0.


What is the operator that cannot be overloaded in c plus plus and java?

conditional operator , size of operator , membership operator and scope resulation operator can not be overload in c++


The IF part of a conditional statement?

The IF part of a conditional statement sets the condition or criteria that needs to be met for the subsequent action to occur. It is the part that is evaluated as either true or false, determining the flow of the statement.


Can you use conditional operator in cout statement?

Yes, but 'cout' is not a statement! Examples for statements: null-statement, block, expression, if-else, while, do-while, for, continue, switch, break, return.


Can you Compare while and do-while?

The 'while' statement evaluates its expression at the beginning of the loop, while a 'do while' statement evaluates its expression at the end of the loop. The 'while' statement might execute no times. The 'do while' statement will execute at least one time. It depends on what you want to do, and on how you want to use the side effects, if any, of the expressions in the expression. (Before or after)


What is condional operator?

Conditional Operator- Its the only ternary operator in c/c++.- Its syntax is-(condition)?statement1:statement2;-Shruti Jain