40

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

Will any veteran C++ programmer please help me out?

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
Robin Rodricks
  • 110,798
  • 141
  • 398
  • 607

10 Answers10

68

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;
lc.
  • 113,939
  • 20
  • 158
  • 187
  • 4
    Furthermore, if x were a const int in your examples, the ternary version would be the only choice that compiles. – Drew Dormann Dec 25 '08 at 21:48
  • Wouldn't it be better to initialize Three? – Jonathan Leffler Dec 26 '08 at 07:41
  • 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. – lc. Dec 26 '08 at 08:18
12

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.

magcius
  • 636
  • 3
  • 7
4

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

Mykroft
  • 13,077
  • 13
  • 44
  • 72
2

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.

edef
  • 743
  • 4
  • 13
1

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;
Robin Hsu
  • 4,164
  • 3
  • 20
  • 37
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.

Daniel
  • 878
  • 6
  • 5
  • 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 – Adam Erickson Mar 09 '18 at 05:36
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

HARSH BAJPAI
  • 45
  • 12
0

simply you can write this as

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

THIS IS SAME AS:

if(A>B)

 C=A;

else

 C=B;
Ravi Kumar Yadav
  • 193
  • 2
  • 15
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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 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. – Remy Lebeau Oct 01 '20 at 05:46
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.

Tushar03
  • 11
  • 1