I've always wondered how to write the "A ? B : C" syntax in a C++ compatible language.
I think it works something like: (Pseudo code)
If A > B C = A Else C = B How can I achieve this?
It works like this:
(condition) ? true-clause : false-clause It's most commonly used in assignment operations, although it has other uses as well. The ternary operator ? is a way of shortening an if-else clause, and is also called an immediate-if statement in other languages (IIf(condition,true-clause,false-clause) in VB, for example).
For example:
bool Three = SOME_VALUE; int x = Three ? 3 : 0; is the same as
bool Three = SOME_VALUE; int x; if (Three) x = 3; else x = 0; It works like this:
expression ? trueValue : falseValue Which basically means that if expression evaluates to true, trueValue will be returned or executed, and falseValue will be returned or evaluated if not.
Remember that trueValue and falseValue will only be evaluated and executed if the expression is true or false, respectively. This behavior is called short circuiting.
In c++ there's no actual if part of this. It's called the ternary operator. It's used like this: <boolean statement> ? <result if true> : <result if false>; For your example above it would look like this:
C = A > B ? A : B; This article on wikipedia also discusses it: http://en.wikipedia.org/wiki/Ternary_operation
No one seems to mention that a result of conditional operator expression can be an L-value in C++ (But not in C). The following code compiles in C++ and runs well:
int a, b; bool cond; a=1; b=2; cond=true; (cond? a : b) = 3; cout << a << "," << b << endl; The above program prints 3, 2
Yet if a and b are of different types, it won't work. The following code gives a compiler error:
int a; double b; bool cond; a=1; b=2; cond=true; (cond? a : b) = 3; cout << a << "," << b << endl; I would say the ? is a short-cut. However, some "hard-core" programmers tend to say write it out the long way so in future cases, people can easily read and modify code.
For example, if you write
int a = b<c ? b : c; Some people claim that it's clearer to write:
if(b<c) a = b; else a = c; Because in future cases, people can catch it. Of course, a simple b<c ? b:c is easy to catch, but sometimes complex operations are put in and it can be hard to spot.
a = ifelse(b<c, b, c). In PHP, it would be $a = ($b<$c) ? b : c;. I would prefer ternary operators for simple cases, such as one-line statements. For multi-line statements, I would use the traditional way for cleaner logical structure.IT IS QUITE SIMPLE IT'S BASIC SYNTAX IS: expression1?expression2:expression3;
If expression 1 is hold true then expression 2 will hold otherwise expression 3 will hold.
example:
hey=24>2?24:34;
here as condition is true value of 24 will be assigned to it. if it was false then 34 will be assigned to it
This is called a "Ternary operator", and the ? and : are another way of writing an if-else statement.
Look at the "Example #1" in this Codepen, and un-comment it... you'll notice what it does.
Now comment "Example #1", un-comment "Example #2", and see what happens. The code does the exact same thing... but this time with only 5 lines of code. Notice how whatever appears before the ? sign is the if (conditional), and whatever comes AFTER the : is the thing to be executed.
But what if you have a conditional that requires an "else-if" (with more than 2 possible outcomes) like the one in the Codepen's "Example #3" (which adds the possibility of the user writing a specific wrong answer)? Then the ternary operator might not be as useful, but you can certainly concatenate several conditions with the ternary operator, like on this ES6 fizzbuzz example.
or just read it out the same way as its verbal counterpart :
if-?-then-:-else
expression ? trueValue : falseValue - when something far less verbose suffices