mardi 3 octobre 2017

c - if + else if + else in one line?

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?

Aucun commentaire:

Enregistrer un commentaire