How about using a do
with breaks. This is actually a sneaky way of doing a goto
though you can think of it as a filter composed of several if
statements in which the default is the last bit if none of the if
statements are hit.
parameters: a, b
do {
if (a > 5)
{
Print("Very well, a > 5");
if (b > 7)
{
Print("Even better, b > 7");
break;
}
}
Print("I don't like your variables");
} while (false);
EDIT - On repurposing language
A number of people have objected to this specialized use of a do while to solve a particular problem. The main objection seems to be that it appears to be a loop but isn't really a loop so this construct falls into a kind of uncanny valley of loop use. In other words, "It just ain't natural."
And I agree that it is an uncommon usage and there really should be comments to identify that this is a filter using a do while to create a block allowing the use of break statements whenever a decision tree branch end point is reached. That is really what this is, a hard coded forward traversal decision tree without backtracking made up of a series of decisions. At any point in the decision process we can break out with a decision or fall through with a default decision including indicating no decision at all.
One could say that any source that requires comments to be understandable is not good code. On the other hand, the reason almost all programming languages have some way of inserting comments is because annotating source code is extremely helpful when you come back six months later to make a change.
The nice thing about this approach is that it creates a local scope so that variables required during the decision process can be constructed and destructed properly.
In some ways it is somewhat like a lambda, to which I doubt anyone would object and it can be used in languages that do not support a lambda. In another way it is some what similar to a try catch.
Perusing the web one can find quite a few articles in which someone uses a programming language in a way different from it's original design intent such as this article on using C++ in a functional programming style or this online book on using object oriented practices with the C programming language.
All programmers have certain styles or language use habits. One good thing that can come from source code reviews and reading the source code of others is learning about a different way of using a programming language.
This is not tricky code like one would find as an entry to the Obfuscated C Programming contest. It is quite straightforward.
Edit: Better than a goto?
One question about this unusual use of a do while is, "Why not just use a goto?" Reading Dijkstra's essay of Go To Statement Considered Harmful, as well as this blog discussion on the essay and the goto statement, we can see there are several nice characteristics about using a loop with break statements which are not characteristics of a goto and its associated label.
The main characteristic, especially with this example is the one way flow in which there is a definite beginning and a definite end. There is no danger of inadvertently changing the program flow by moving the goto label. There is no danger of somewhere else in the function using the goto label as an opportune place to jump creating a dependency that was not originally intended. Reading the code, every programmer knows that where there is a break, you are leaving the loop and that on leaving the loop, you go to the source line after the loop close. The result is that you have a nice clean knowledge chunk, something that can be labeled as "figure out the text to print"