Conditional or ternary Operator in Different Languages

? is the conditional operator or the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide; which value should be assigned to the variable.

SyntaxExampleExplanation
C/Java/Javascript/C# conditionalExpression ? expression1 : expression2 days= (February == ‘1’) ? 29 : 28; If February is equal to 1 then days=29 otherwise 28
C/Java/Javascript/C# isMember ? “$4.00” : “$10.00”;if is Member, assign the value $4.00 otherwise $10
PHP If Condition is true ? Then value X : Otherwise value Y $result = ($a > $b ) ? $a :$b; If condition is true then assign a to result otheriwse b

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at WordPress.com.

Up ↑

Design a site like this with WordPress.com
Get started