Sometime one have to implement a series of if/else checks. In old-days goto was the sandard instrument for this.
As goto is a no-go in many code style guides, I sometimes use loops as a replacement e.g.:
do
{
if (a)
{
doA();
break;
}
//...
if (z)
{
doZ();
break;
}
}while(false);
//all break jump here
Is it a good approach? Is there a good C++ pattern (e.g. using templates, inheritance, etc.) for implementing this without too much overhead?
As conditions seem unrelated, else if
is an option:
if (a) {
doA();
} else if (b) {
//...
} else if (z) {
doZ();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With