Page 1 of 1

case/break

Posted: Sun Oct 25, 2009 11:34 pm
by phaworth
Maybe it's just me, but after using hundreds of switch/case commands, I think I've found one use for have one case statement drop through to the next by omitting the break statement. Conversely, I've been bitten a lot by forgetting to include a break statement.

I don't suppose there's a setting/trick/technique that makes it unnecessary to include break statements....

Pete

Posted: Mon Oct 26, 2009 1:14 am
by Mark
Hi Pete,

There is no way to make the break unnecessary. Break is very useful though:

Code: Select all

switch theNumber
  case 1
  case 3
  case 5
  case 7
  case 9
    put "The number was odd"
  break
  default
    put "The number was even"
end switch
This is a silly example, but it is provides a good way to illutrate the mechanism.

Best,

Mark

Posted: Mon Oct 26, 2009 1:31 am
by phaworth
Thanks Mark. I understand that it can be useful sometimes and I have made use of it but I'd say 90% or more of the time, I have to use break so to me it would be better if the default was not to drop through and there was another command/keyword that could be used when you do want to drop through to the next case, something like 'continue' perhaps.

Anyway, it's not a big deal, I guess I'm new enough to Rev that I still get annoyed with myself when I forget to use break and weird things hapen!

Pete

Posted: Mon Oct 26, 2009 3:33 am
by mwieder
Pete-

Almost every language that supports a switch statement uses this syntax, so better get used to it. While I agree that I use the break statement a lot more than I drop through (I think 90% is an underestimate), that's just the way it is.