(I searched for "else if range", but did not come up with any posts that answered my question).
When using if .. elseif .. else to select what to do based on a variable that is in certain (mutually exclusive) ranges, I would like to explicitly specify the ranges:
int num = 55; String message; if (num >= 20) { // update message variable message = "Bigger or equal to than 20"; } else if (10 <= num && num < 20) { // update message variable } else if (0 <= num && num < 10) { // update message variable } else if (num < 0) { // update message variable } System.out.println(message);
But all the textbooks and lectures that I see write this example:
int num = 55; String message; if (num >= 20) { // update message variable message = "Bigger or equal to than 20"; } else if (num >= 10) { // update message variable } else if (num >= 0) { // update message variable } else { // update message variable } System.out.println(message);
I understand why they should use else at the end (i.e. do not let the Java compiler think that a variable like a message cannot be initialized if this variable was primitive), but given that all the tutorials and lectures show a different style, there is Are there any problems caused by explicit spelling of ranges for all other conditions as I like?
silph source share