Re: New Switch Idea
- Posted by DerekParnell (admin) Mar 27, 2009
- 1113 views
jeremy said...
Please comment as to what you think about this idea:
I think that the syntax needs to be ...
switch <expr> [(with | without fallthru)] [label "name"] [do] case <valuelist> [:] <statements> case <valuelist> [:] <statements> case <valuelist> [:] <statements> . . . case else <statements> end switch
Where
- <expr> is an expression that evaluates to any Euphoria object
- If there is no 'fallthru' clause then 'without fallthru' is assumed
- <valuelist> is a comma-separated list of one or more discrete values that <expr> can equal to.
- <statements> is a block of one or more euphoria statements. A zero length statement block is not allowed.
- If 'without fallthru' is in operation, once <statements> block is executed, control passes to the 'end switch' line. Any 'break' statements are ignored.
- If 'with fallthru' is in operation, once a <statements> block is executed and is does not end in a 'break' statement, control passes to the next (if any) lexical <statements> block, otherwise control is passed to the 'end switch'.
- 'goto' can be used to manually change control flow within a switch.
Examples:
integer X = 1 switch X without fallthru case 1 ? 1 case 2 ? 2 end switch -- output: -- 1
integer X = 1 switch X with fallthru case 1 ? 1 case 2 ? 2 end switch -- output: -- 1 -- 2
integer X = 1 switch X with fallthru case 1 ? 1 case 2 ? 2 break case 3 ? 3 end switch -- output: -- 1 -- 2
integer X = 1 switch X case 1 ? 1 case 2 ? 2 end switch -- output: -- 1
object X = "three" switch X case 1,2,"three",4.112233, 5 ? 1 case 6,7,8,9,10 ? 2 end switch -- output: -- 1