Ternary conditional operator: Difference between revisions

Content deleted Content added
Line 36:
Originally from ALGOL 60 the conditional assignment of ALGOL is:
 
''variable'' := '''if''' ''condition'' '''then''' ''expression_1'' '''else''' ''expression_2'';
 
{{code|?:}} is used as follows:
Line 44:
The ''condition'' is evaluated ''true'' or ''false'' as a [[Boolean expression]]. On the basis of the evaluation of the Boolean condition, the entire expression returns ''value_if_true'' if ''condition'' is true, but ''value_if_false'' otherwise. Usually the two sub-expressions ''value_if_true'' and ''value_if_false'' must have the same type, which determines the type of the whole expression. The importance of this type-checking lies in the operator's most common use—in [[Conditional (programming)|conditional]] [[Assignment (computer science)|assignment]] statements. In this usage it appears as an [[Expression (programming)|expression]] on the right side of an assignment [[Statement (programming)|statement]], as follows:
 
''variable'' = ''condition'' ? ''value_if_true'' : ''value_if_false'';
 
The ?: operator is similar to the way conditional expressions ('''if-then-else''' constructs) work in [[functional programming]] languages, like [[Scheme (programming language)|Scheme]], [[ML (programming language)|ML]], [[Haskell]], and [[XQuery]], since if-then-else forms an expression instead of a statement in those languages.