Counter function
Dec 16, 2013 at 4:49am UTC
I am writing a class for a counter. There is a decreaseCount function in that class, which will decrease the count by 1 if the count is 1 larger, and do nothing if the count is already zero.
And here's my code
1 2 3 4 5 6 7
void CounterType::decreaseCount()
{
if (count > 0)
count--;
else
;
}
For me, line 5 and 6 seems strange.
I wonder if there is a better way to do so?
Dec 16, 2013 at 4:52am UTC
Get rid of those lines
1 2 3 4 5
void CounterType::decreaseCount()
{
if (count > 0)
count--;
}
Dec 16, 2013 at 5:15am UTC
If you really want to do something with the else, you can throw an exception or just do as @ats15 suggested and get rid of the else part
Dec 16, 2013 at 7:14am UTC
Thanks! I though one "if" must pair up with one "else"!
Topic archived. No new replies allowed.