Re: New switch/case idea
- Posted by DerekParnell (admin) Apr 01, 2009
- 1826 views
The with fallthru clause helps in those rarer situations in which a given input is required to trigger multiple case blocks.
Does that help?
No.
Your first example is in error because x != 2,3,4, or 5, with or without fallthru. The concept of fallthru is great where a situation is like:
X=1 switch X with fallthru do case 1: ? 1 case 2: ? 2 case 3: ? 3 case 1,2: ? "1 or 2" end switch
OUTPUT: 1 "1 or 2"
You said:
The with fallthru clause helps in those rarer situations in which a given input is required to trigger multiple case blocks.
But the problem is, in the first example you gave, x is not 2,3,4, or 5, so they should not execute, just as if you had
X=1 switch X do case 2: ? 2 case 3: ? 3 case 4: ? 4 end switch
OUTPUT: none!
I really do understand what you are saying, Kat.
Please try to understand what I'm saying. The switch statement is not syntax sugar for an if - elsif - endif series. The case clause is not an executing statement, it can be more thought of as a label.
Here are the examples written out in long hand.
-- WITH FALLTHRU EXAMPLE --- X=1 temp = find(x, {1,2,3,4,5,6,7}) if temp = 1 then goto "case_1" end if if temp = 2 then goto "case_2" end if if temp = 3 then goto "case_3" end if if temp = 4 then goto "case_4" end if if temp = 5 then goto "case_5" end if if temp = 6 then goto "case_6" end if if temp = 7 then goto "case_7" end if goto "case_end" label "case_1" ? 1 label "case_2" ? 2 label "case_3" ? 3 label "case_4" ? 4 label "case_5" ? 5 goto "case_end" -- break statement label "case_6" ? 6 label "case_7" ? 7 label "case_end" -- OUTPUT: 1 2 3 4 5
Of course, the actual 'goto' is optimised in Euphoria to be more like a computed goto.
-- WITHOUT FALLTHRU EXAMPLE --- X=1 temp = find(x, {1,2,3,4,5,6,7}) if temp = 1 then goto "case_1" end if if temp = 2 then goto "case_2" end if if temp = 3 then goto "case_3" end if if temp = 4 then goto "case_4" end if if temp = 5 then goto "case_5" end if if temp = 6 then goto "case_6" end if if temp = 7 then goto "case_7" end if goto "case_end" label "case_1" ? 1 goto "case_end" -- because of no-fallthru default label "case_2" ? 2 goto "case_end" -- because of no-fallthru default label "case_3" ? 3 goto "case_end" -- because of no-fallthru default label "case_4" ? 4 goto "case_end" -- because of no-fallthru default label "case_5" ? 5 goto "case_end" -- break statement label "case_6" ? 6 goto "case_end" -- because of no-fallthru default label "case_7" ? 7 goto "case_end" -- because of no-fallthru default label "case_end" -- OUTPUT: 1
You are not being forced to use the fallthru clause if its against your coding needs or principles. In the same way the "goto" is one of your coding tools but others refuse to use it, the "with fallthru" clause is a tool that some may choose or use or not, as they see fit.
We have given you the goto because of your persistant pressure and valid arguments for its inclusion. Please see that with fallthru is another coding style much like the goto statement.