java - How can I avoid using more than a single "break" statement in a loop? -
for code quality reason, refactor code little bit in order use 1 break
statement in loop. not sure can way sonarqube aking me...
here's code :
for (integer integer: integerkey.keyset()) { if (map.containskey(integerkey.get(integer))) { typevalue value = map.get(integerkey.get(integer)); sb.append(integerkey.get(integer)).append(":"); sb.append(encodevalue(value)); sb.append("|"); if (integerkey.get(integer).equals(min)) { break; } } else if (integerkey.get(integer) <= min){ typevalue value = map.get(min); sb.append(min).append(":"); sb.append(encodevalue(value)); sb.append("|"); break; } else { sb.append(integerkey.get(integer)).append(":"); sb.append("0"); sb.append("|"); } }
i same thing using 1 break
not sure can write 1 if
condition in case instead of if-elseif-else
.
any ideas ?
thanks.
you define variable break-condition , include for-loop condition:
boolean endloop = false; (iterator<integer> keys = integerkey.keyset(); keys.hasnext() && !endloop; ) { integer integer = keys.next(); if (map.containskey(integerkey.get(integer))) { ... if (integerkey.get(integer).equals(min)) { endloop = true; } } else if (integerkey.get(integer) <= min){ ... endloop = true; } else { ... } }
or declare local variable in loop set true if loop should left break:
for (integer integer: integerkey.keyset()) { boolean endloop = false; if (map.containskey(integerkey.get(integer))) { ... if (integerkey.get(integer).equals(min)) { endloop = true; } } else if (integerkey.get(integer) <= min){ ... endloop = true; } else { ... } if (endloop) break; }
Comments
Post a Comment