4

I just got a question idea after having misunderstood a friend's statement.

My friend told me: I just taught a colleague how to do a if/else in one line in c.

Example:

int i = 0;
i < 0 ? printf("i is below 0") : printf("i is over or equal to 0");

For now, nothing new, it's called a ternary and most people know about that kind of statement BUT I first understood that:

I just taught a colleague how to do a IF / ELSE IF / ELSE in one line. Since I don't / didn't know that doing such a thing is possible I tried to do something like

int i = 0;
 i < 0 ? printf("i is below 0") : i == 0 ? printf("i equal 0") : printf("i is over 0");

Is it actually possible to do a if / else if / else "ternary". Or is there a way to do such a thing without having an horrible piece of code?

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Cjdcoy
  • 317
  • 1
  • 2
  • 12
  • 2
    This is not related to your question but technically you could an if else if else statement on one line without using ternary anything. Of course that would be absurd but the ternary isn't there just because it's on one line. Its a syntactic and readability convenience. So don't put so much emphasis on "in one line" The compiler doesn't care if you put everything on one line or format it a certain way with spaces. I just wanted to add this in even if you already knew this. Trust me many dont. good luck – RSon1234 Oct 03 '17 at 08:23
  • 1
    C disregards all whitespace characters that aren't required to separate preprocessing tokens. What's this great "virtue" of a doing things in a "single line"? You can do an `if` statement in a single line too. – StoryTeller - Unslander Monica Oct 03 '17 at 08:29
  • The function `printf` returns a value, but your code would make no sense when calling a function that does not. – Weather Vane Oct 03 '17 at 08:32
  • "in one line" has no benefit whatsoever. Instead you should focus on writing readable and fast code. – Lundin Oct 03 '17 at 08:33
  • To add/modify what @Lundin says above, it has negative benefit. Any developer who is stepping though the code with their debugger, in an attempt to find some elusive bug, sees these 'clever' one-liners and wishes umm... 'really bad things' on the author. – Martin James Oct 03 '17 at 10:05

3 Answers3

15

If you see e.g. this conditional expression reference you can see that the format of a "ternary expression" is

condition ? expression-true : expression-false

All three parts of the conditional expressions are, in turn, expressions. That means you can have almost any kind of expression, including nested conditional (ternary) expressions in them.


It should be noted that conditional expressions might make the code harder to read and understand, especially if used badly or if one attempt to put too much logic and nesting into the expressions.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
9

This is definitely valid.

Or you could try something like this -

printf(i < 0 ? "i is below 0" : i == 0 ? "i equal 0" : "i is over 0");
Superman
  • 159
  • 1
  • 6
0

C has both statements and expressions. There are two different kinds of syntactical things. BTW lines don't matter much in C (except for the preprocessor).

Expressions (like f(1,x+y) or even x=y++) are a special kind of statements (the most common one).

As an extension to C, the GCC compiler adds statement expressions, beyond what the C11 standard (read n1570) defines. Please download then read that n1570 repoort.

if is for conditional statements but the ternary ?: operator is for expressions (with all three operands being sub-expressions).

Some programming languages (notably Lisp, Haskell, Scheme, Ocaml) have only expressions and don't have any statements.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547