21

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?

sherrellbc
  • 4,650
  • 9
  • 48
  • 77
  • This definitely works in C and compiles using gcc. – sherrellbc Sep 05 '13 at 21:24
  • 2
    The `return` statement can definitely not be put inside an expression. –  Sep 05 '13 at 21:26
  • Okay, I was just writing that as an example. The syntax is correct, my example implementation was faulty; that is not the point of this question. Thank you anyway. – sherrellbc Sep 05 '13 at 21:28
  • 5
    `?:` is *not* "a shorthand IF-ELSE statement" - it's an *operator*. See e.g. http://stackoverflow.com/questions/18638915/what-is-the-and-sequence-actually-called/18638981 – Paul R Sep 05 '13 at 21:29
  • @PaulR, Thanks for clearing that up. Although it was referred to on that page by at least one person as both a short-hand if-else and ternary operator. – sherrellbc Sep 05 '13 at 21:34
  • 1
    It's a common misunderstanding. There are *some* cases where you can replace if/else with an expression using `?:`, but it's a limited subset. – Paul R Sep 05 '13 at 21:36
  • Yeah, I see that now. I commented below on the post. That's all I was wondering, really. – sherrellbc Sep 05 '13 at 21:38
  • @ouah You're wrong. It works with C – ha9u63a7 May 23 '14 at 07:27

2 Answers2

17

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.

Jeremy
  • 22,188
  • 4
  • 68
  • 81
  • Thanks! That's all I was asking. I was just wondering if there was a way to not implement the either part if all you care about is when the condition evaluates a certain way, otherwise do nothing. Although, I suppose it does make sense in the case of an assignment using this statement. If the case was false, there would be no defined assignment. – sherrellbc Sep 05 '13 at 21:37
  • gcc allows some sort of special case for this but it's a non-standard extension. E.g. `retval = retval ?: desc.error;` - see http://stackoverflow.com/questions/7814694/bizarre-use-of-conditional-operator-in-linux – Paul R Sep 05 '13 at 21:38
  • @PaulR I remember reading that somewhere, but I never looked into it. It doesn't seem that useful. – Jeremy Sep 05 '13 at 21:39
  • No - it's just *slightly* more compact than an if statement, but I tend to avoid non-standard extensions such as this as my code needs to be portable. – Paul R Sep 05 '13 at 21:40
11

Question is whether we can somehow write the following expression without both then and else parts

(integer == 5) ? (THENEXPR) : (ELSEEXPR);

If you only need the then part you can use &&:

(integer == 5) && (THENEXPR)

If you only need the else part use ||:

(integer == 5) || (ELSEEXPR)
  • For the latter ones it has to be clear, that no assignment will be done for the 'other case'. In my case, I was looking if it is possible to shorten `int number=0; integer =2; number = (integer == 5) ? ( then reset ) : ( keep whatever )`. But `number` stays 0. – CanO Nov 07 '17 at 16:48
  • If embedded as the Right Value of an assignment, then the assignment must take place and so the 'other case' of the expression evaluates to and assigns the value of the shortcut condition: 1 for || and to 0 for &&. As "a shorthand IF-ELSE statement" (OP query) the value would be discarded and the assignment should be part of the then part and/or the else part (whichever exist). Alternatively you can use n = (i==5)? 0 : n, but why not just say if (i==5) n=0; which is nice and short (shorter - even with single char var). – David M W Powers Nov 22 '17 at 00:18