Is this a valid / sound way of resetting a counter if a condition is not met? It is the most compact way I could think of.
int counter = 0;
int a,b;
// Do .. and assign a and b
counter = ((a<b) ? counter++ : 0);
Is this a valid / sound way of resetting a counter if a condition is not met? It is the most compact way I could think of.
int counter = 0;
int a,b;
// Do .. and assign a and b
counter = ((a<b) ? counter++ : 0);
You are already assigning to counter
, so don't use ++
as well.
counter = condition ? (counter + 1) : 0;
The behaviour of counter = (condition ? counter++ : 0);
is undefined as there's no sequencing point. (The ternary is not sequenced, and neither is assignment).
It's similar in form to i = i++;