• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can I do this with a Switch case?

 
Mario Gimenez
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I need use a switch case but I don't know if this is correct.


I won't do write each case because I don't know if this ok.

thanks.
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


where num must be a type compatible with an int (byte,short,char,int) or, as java6, an enum

case 1..99: does not exist in java 6

Bye
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case, an if-else if-else if would seem better:
With these ranges there are other tricks using % 10, % 100 and % 1000 as well:
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mario Gimenez wrote:Hi. I need use a switch case...

Do you really need to use switch/case?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



(But we always write clean, readable code, so we would never do this in practice. Right?)
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's just cruel.
 
Mike Simmons
Ranch Hand
Posts: 3041
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)

You are so right of course. I was so busy about trying to prevent nested ifs that this slipped by. "&& num / 10 == 0" and "&& num / 100 == 0" should fix this. Or just stick with the simple range checking.

Mike Simmons wrote:There's something amusing in the fact that a guy named Prime overlooked this.

 
adam smith ii
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:In this case, an if-else if-else if would seem better:
With these ranges there are other tricks using % 10, % 100 and % 1000 as well:


As well as other remarks about 0 remainder, will this code accomplish anything for any number not ending in 0? That is to say, the comparison of (num % (power of 10)) to zero will always be false using the remainder operator.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that mistake has already been noticed

It might work better with / than %
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic