Hi. I am currently working on a project with quite a few simple if statements. Having used both Java and C++ I know that a compact if statement is a simple and elegant solution to many lines of if statements. However when ever I try the following statements I get a error that says “Expressions in statements must only be executed for their side-effects”. Any idea why this is happening?
isRight ? IdleRight() : IdleLeft();
The ternary operator is intended for assignments, and won’t let you just throw the value away. For example,
isRight? 1 : -1;
does nothing, so it would get the same error. You could say that using functions is different since they’re not necessarily “doing nothing”, but the compiler doesn’t see it that way and won’t let you use ternary operators like that.
Not necessarily. Being more compact doesn’t mean it’s more elegant or readable; in fact the opposite can be true. Some people think it’s bad style and won’t use it at all. I don’t go that far, but I only use it where I consider that it makes sense, not just because it’s more compact.
–Eric
Awesome! I assumed it was something along those lines. I like trying to use obscure operators to better familiarize myself with them. Thanks for the help ![]()