Question: I just saw this block of code on the [Wikipedia](http://en.wikipedia.org/wiki/%3F:#.3F:_in_style_guidelines) article on conditional operators:

```
Vehicle new_vehicle = arg == 'B' ? bus      :
                      arg == 'A' ? airplane :
                      arg == 'T' ? train    :
                      arg == 'C' ? car      :
                      arg == 'H' ? horse    :
                      feet;

```

I've changed the code a little, but the idea is the same. Would you find this use of the conditional operator acceptable? It's much more concise than the `if`-`else` construct, and using a switch would definitely open up a whole new set of opportunities for bugs (fall-throughs anyone?). Also, `if`-`else`s and `switch` can't be used as R-values, so you'd have to create the variable first, initialize it and then assign as necessary.

I for one really like this, but I'm wondering what others think.

But the formatting is essential.

EDIT: I still like this. But I understand those who say "the `switch` statement was made for this". OK, maybe so. But what if the conditions are function calls that return `bool`? Or a million other things you can't switch on.

Are you switch lovers really trying to convince me that a huge `if`-`else` chain is better? Yes, programmers who don't know how to use the conditional operator will not understand this. They should learn how to use it. It's not arcane.

Answer: 
Not only is there nothing wrong with it, it communicates the intent of the operation in the most concise and clear way possible.

Replacing with if else, or switch construction requires that the snippet 

```
"new_vehicle =  "

```

be repeated in every instance, which requires that the reader read every repeating instance of it to ensure that it is in fact the same in every instance..
The conditional operator version is clean, simple, and it's immediately obvious to anybody who knows C or C++ what is happening. Its other virtue is that it returns a value immediately, which means it can be put in the initialization (like this example is).

A switch statement would be more clumsy. It would require that the variable be declared and then initialized, usually a bad idea if it can be avoided. It would take more typing, and would have more places for bugs to creep in. It wouldn't be as clear, since it would be necessary to look at each case to see that it said something like `new_vehicle = foo; break;`.

If you're going to do this look up here only, then having the conditional version right there is good, as it immediately shows what's happening. If it's going to occur more than once, consider putting it in a function, so that there's only one place to update if anything changes (such as, say, 'R' for carriage or 'L' for helicopter).