cif-statementshortcut

IF-ELSE statement shortcut in C


C has the following syntax for a shorthand IF-ELSE statement

    (integer == 5) ? (TRUE) : (FALSE);

I often find myself requiring only one portion (TRUE or FALSE) of the statement and use this

    (integer == 5) ? (TRUE) : (0);

I was just wondering if there was a way to not include the ELSE portion of the statement using this shorthand notation?


Solution

  • The operator ?: must return a value. If you didn't have the "else" part, what would it return when the boolean expression is false? A sensible default in some other languages may be null, but probably not for C. If you just need to do the "if" and you don't need it to return a value, then typing if is a lot easier.