46

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?

11 Answers 11

74

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; 
Sign up to request clarification or add additional context in comments.

4 Comments

Furthermore, if x were a const int in your examples, the ternary version would be the only choice that compiles.
Wouldn't it be better to initialize Three?
Shmoopty - Very true, because you can only initialize a constant when it's declared. Jonathan - Yes, yes it would. Edited. Maybe I should have done "//Three is a bool" instead.
C = (A > B) ? A : B;
14

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.

Comments

5

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

Comments

4

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; 

Comments

3

I assume you mean stuff like a = b ? c : d, where b is the condition, c is the value when b is true, and d is the value when b is false.

Comments

1

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.

1 Comment

Your example is the most straightforward for typical use cases. The correct term is "ternary operators" and they are very easy to understand and use a similar syntax in many languages. In R, for example, the same statement would be 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.
1

simply you can write this as

C=(A>B)?A:B;

THIS IS SAME AS:

if(A>B) C=A; else C=B; 

Comments

0

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

Comments

0

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.

1 Comment

StackOverflow entries are expected to be self-contained. Please do not rely on external sites to provide relevant information. Links break over time. Copy the appropriate portions directly into your answer. In this case, the code examples you are referring to. Or provide your own code examples.
0

like this is our condition -> A?B:C So according to the condition if 'A' is True then it prints 'B' or if 'A' is false it prints 'C'. In other words we can also say that it is an alternative way of if else condition.

Comments

0

or just read it out the same way as its verbal counterpart :

if-?-then-:-else

2 Comments

This is more a comment to me unless to put actual c++ code in with an example perhaps you can update your proposed answer.
@MarkSchultheiss : others have already shown the actual codes no need for me to reinvent the wheel. I was trying to showcase a simpler mnemonic than expression ? trueValue : falseValue - when something far less verbose suffices

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.