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.