In
switch (ch) {
case '0': Value v = compute(); v = invert(v); accumulate(v); break;
case '1': v = compute(); accumulate(v); break;
default:
}
the compute and accumulate code pieces are common.
Java allows fall-through cases
Value v = compute();
switch (ch) {
case '0': v = invert(v);
case '1': accumulate(v); break;
default:
}
Unfortunately, compute() is executed for ch values outside the {0,1}. Can I share both without the side effects and performance penalty?
By performance penalty I mean that I can always rewrite the switch with more flexible if-then statements.
if (ch == '-') continue; // next loop iteration
assert (ch == '1' or ch == '0');
v = compute();
if (ch == '1') v = invert(v);
accumulate(v);
However, this demands at least one branch statement more than single switch. And, every such statement adds 10x performance slowdown. Yet, I'm gonna use the construction in a loop
for (char ch : computeBits()) // ch can be '0','1','-'
switch_block;
So, I prefer to stay with a single switch.

ifperformances depend on the test inside()- @Kinjal gives you a very good Java solution whereswitchsequence is replaced by?:syntaxe – cl-r Nov 27 '12 at 13:42iftests per valid character. Ternary operator,?:, isifactually, despite in some respect it may flavour more like datapath rather than control. – Val Nov 27 '12 at 15:03