Assuming that there is only two possible values 5 or 7, Just do the following:
int end = boolean1 ? 5 : 7;
for(int i = 0; i < end; i++){
//do this
}
Create a variable (e.g., end
) for loop condition and set the value accordingly.
The same idea applies if there is more variables:
int end = 0;
if(variable1) end = ... ; // some value
else if(variable2) end = ...; // some value
...
else if(variableN) end = ...; // some value
for(int i = 0; i < end; i++){
//do this
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…