Reputation:
I am looking to write conditional code without using (if/else/?/while etc...)
in C language.
For example the following code:
if (Num>6) printf("T");
can be converted to (And still do the same task):
Num>6 && printf("T")
but the following:
bool larger;
if (Num>6) larger=true;
can't be replaced with:
bool larger;
Num>6 && larger=true;
Since lvalue is required as left operand of assignment
Any help? (I think &&
operation and the use of bool would be helpful)
Upvotes: 1
Views: 216
Reputation: 41753
Add parentheses around:
bool larger;
Num > 6 && (larger = true);
Why? Because =
has lower precedence than &&
and >
. That means the expression is parsed as
((Num > 6) && larger) = true;
which is an invalid assignment
Upvotes: 2