if statement vs ?:

What is the difference between this:
1
2
if(a <= b) max = b;
else max = a;

and this:
 
max = (a <= b) ? b : a;


Other that the second conditional expression can be used in const expressions.
Last edited on
max = (a <= b) ? b : a is an expression, and max = (a <= b) ? b : a ; is an expression statement.
Normal rules for evaluation of expressions apply; implicit conversions are performed.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

int main()
{
    struct max_
    {
        max_( const char* ) : value(123) {}
        max_( const std::string& = {} ) : value(-9999999) {}

        int value ;
    } max ;

    const std::string a = "abcd" ;
    const char* b = "efgh" ;

    if(a <= b) max = b;
    else max = a;
    std::cout << max.value << '\n' ; // 123

    max = (a <= b) ? b : a; // implicit conversion to std::string
    std::cout << max.value << '\n' ; // -9999999
}

http://coliru.stacked-crooked.com/a/32c56d718082d01c
@JLBorges
Thank you
You have learned c++ using stroustrup's TC++PL right?
Last edited on
> You have learned c++ using stroustrup's TC++PL right?

I've read (and still preserve) the first edition of the book;
and each time I read some part of TC++PL (any edition), I tend to learn something.

Most of my initial learning was from (the now obsolete)
'The Annotated C++ Reference Manual' (Stroustrup and Ellis).
Last edited on
Ok thank you for these informations
if else gives you decisions in code sequence,

?: gives you decisions in the middle of expressions.

in my experience it is one of, if not the, most misused operators out there.

i often see it used instead of if, because its less text some coders think it will run quicker :|

this is the most common example, remember, if you have two "=" in there then somethings gone wrong. i encounter this often when reviewing others code, a direct translation from an if statement.

1
2
3
(age >= 18)
    ? adult = true
    : adult = false;


when really it should read as follows, ?: are for describing your optional results, not doing the assignment.
1
2
 
adult = (age >= 18) ? true : false;


can be used in const expressions

IS an expression. IMHO "can be" is dangerous and implies its ok to use it outside of expressions, if you start using it like an if statement you *will* develop the urge to start nesting them which can really be a pain to read.

Last edited on
Why not simply write adult = (age >= 18); instead of the convoluted adult = (age >= 18) ? true : false;
because i was demonstrating ?: and bool is the simplest type :) I considered using ints when talking about nesting but decided against an example.
This has been a topic I have pondered from time to time. I tend to shy away from the ternary (or conditional) operator because it is less frequently used (and thus less understood) than if statements and can be more easily hidden the the clutter of code when quickly scanning code. Now I will also be concerned with the implicit conversion that @JLBorges brought up now that I realize that this side effect occurs.

I do realize that if/else statements cannot be used when initializing constant values. And the ternary operator can come in handy when initializing members of an object in a constructor initialization list.

Beyond those scenarios, I prefer the more verbose if/else construct because of readability / understandability, ease of debugging, and maintainability. (I'm not hating on ternary users, just stating my preferences).

My question is similar to the @OP's question. Other than initialization of constant values and constructor initialization lists, are there situations where the ternary operator is clearly superior to if/else statements for reasons other than merely more compact code?
Topic archived. No new replies allowed.