In many languages, there is an instruction called break
that tells the interpreter to exit the switch after the current statement. If you omit it, the switch fall-through after the current case has been processed:
switch (current_step)
{
case 1:
print("Processing the first step...");
# [...]
case 2:
print("Processing the second step...");
# [...]
case 3:
print("Processing the third step...");
# [...]
break;
case 4:
print("All steps have already been processed!");
break;
}
Such a design pattern can be useful if you want to go through a serie of transitive conditions.
I understand that this can cause bugs due to unintentional fallthrough if the programmer forgets to insert the break statement, but several languages are breaking by default, and include a fallthrough keyword (e.g. continue
in Perl).
And by design, the R switch also breaks by default at the end of each case:
switch(current_step,
{
print("Processing the first step...")
},
{
print("Processing the second step...")
},
{
print("Processing the third step...")
},
{
print("All steps have already been processed!")
}
)
In the above code, if current_step
is set to 1, the output will only be "Processing the first step..."
.
Is there any way in R to force a switch case to fall through the following case?