marc weber wrote:
Rob Prime wrote:... With these ranges there are other tricks using % 10, % 100 and % 1000 as well:
Ah, but 100 % 10 is 0. (As with 1000, 10000, etc.)
There's something amusing in the fact that a guy named Prime overlooked this. Maybe he's not used to numbers having other factors.
On a related note though, it's worthwhile to pay attention to the ordering of if / else if / else clauses, and avoid needless redundancy. For this example here, I'd rather code it like this:
I replaced "Hola" with three different strings A-B-C, since otherwise the whole construct is pointless. Anyway, organizing the statements this way, we don't need to specify (for example) <=99 for one group, and >=100 for the next. We eliminate needless redundancy, and also make it easier to make alterations if we need to change the cutoff from 100 to, say, 256, in a future release. This way we only need to change one number, instead of two. And we're less likely to have problems because we said < instead of <, or because we refactored num to be a double rather than an int.