How to use conditional operator in c++.
The conditional operator is also called Ternary Arithmetic Operator as it takes 3 Operands. We can use the conditional operator to create short expression that works like if/else statements. The operator consists of the question-mark (?) and the colon (:). The syntax is given below:
Syntax: Conditional expression? True expression: False expression
Example: a < 0 ? b = 100 : c = 200 ;
It works like If-Else statement.
The following expression figure illustrates the roles played by the 3-sub expressions.
Conditional Expression: True Expression: False Expression:
Expression to be tested Executes if the condition Executes if the condition
is true is false
a < 0 ? b=100 : c = 200 ;
The Conditional Operator
The conditional operator can be applied to many other programming problems.
Cout<<”your grade is:”<<score < 60 ? “Fail” : “Pass”);