Re: switch statement

new topic     » goto parent     » topic index » view thread      » older message » newer message
jeremy said...

With a switch statement, once a case evaluates as true, then all further case statements are ignored and a break will cause the switch to be exited. Here is an example:

sequence value = "red" 
 
switch value do 
  case "apple": 
  case "pear": 
  case "orange": 
    puts(1, "It's a fruit!\n") 
    break 
 
  case "yellow": 
  case "blue": 
  case "red": 
    puts(1, "It's a color!\n") 
    break 
 
  case else 
    puts(1, "I have no clue what it is sad\n") 
end switch 
 
-- Output is "It's a color!" 

As for break vs. continue, we choose to keep the normal functionality of a switch as defined in just about every language. It would be pretty difficult for anyone who uses multiple languages or has knowledge of another language, to come to Euphoria and start working with a backward switch. That would be serious cause of bugs and errors.

I think what some people are thinking about is actually a select statement:

sequence value = "red" 
 
select value do 
  case "apple": 
    puts(1, "It's an apple\n") 
  case "pear": 
    puts(1, "It's a pear\n") 
  case "orange": 
    puts(1, "It's an orange!\n") 
  case "yellow": 
    puts(1, "It's yellow\n") 
  case "blue": 
    puts(1,"It's blue\n") 
  case "red": 
    puts(1, "It's red\n") 
  case else 
    puts(1, "I have no clue what it is sad\n") 
end select 
 
-- Output is: "It's red" 

A select statement does not fall-thru. Euphoria does not yet have a select statement, I just was showing the differences.

Jeremy

Ok, I'm beginning to understand, I think.

Fall through can be useful for GENERALIZING tests: red, yellow, blue all more easily resolve to "colors" via switch, wheras "keypressed" would more easily identify "y", "n", or "q" as distinct and different inputs via select.

So, has it been determined that GENERALIZING is more often a programmers intent than DISTINGUISHING?

Dan

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu