Branch coverage measures whether each control-flow alternative is exercised by a test suite. It can expose untested behavior even when line or instruction coverage reaches 100%. section 3.8
A missing branch in a loop
In a loop that scans for "%d", tests may exercise the c == '%' path repeatedly while never making c == '%' false after reaching the condition. The loop can then fail to advance and run forever. Tests such as "%%%%" and "%%d%%%d" exercise different branches and expose the risk. section 3.8
Use branch coverage to check loop behavior, not merely statement execution:
- Test zero, one, and more-than-once iterations. section 3.8
- Inspect missing branches when a loop condition or conditional path is never false. section 3.8
- Check indexOf()-based loops for branches that fail to update the search position. section 3.8
static boolean hasPercentD(String str) {
int pos = 0;
while (true) {
pos = str.indexOf('%', pos);
if (pos == -1 || pos == str.length() - 1) { #1
return false;
}
char c = str.charAt(pos + 1);
if (c == 'd') { #2
return true;
}
if (c == '%') { #3
pos += 2;
}
}
}
Inspect the focused hasPercentD loop and its numbered conditional branches. section 3.8


