Re: New Switch Idea
- Posted by jeremy (admin) Mar 28, 2009
- 1066 views
euphoric said...
DerekParnell said...
integer X = 1 switch X with fallthru case 1 ? 1 case 2 ? 2 end switch -- output: -- 1 -- 2
Is that output correct? If so, can somebody explain again why if the case doesn't match it still runs the code? I think it was explained before but I've forgotten the logic.
I'd expect the output above to be
-- output: -- 1
with fallthru means that the execution will fall right thru any case statements it may find, i.e. pay no attention to them, once (and only once) a case statement caused it to start executing code in the switch. His example is correct.
switch 1 with fallthru do case 1: ? 1 case 2: ? 2 case 3: ? 3 break case 4: ? 4 case 5: ? 5 end switch -- Output is: -- 1 -- 2 -- 3 -- -- If it would have been switch 4, the output would have been -- 4 -- 5
The action you are describing would be without fallthru, i.e. switch does not just fall right through the next case statement. The next case statement after a match was found is treated as a break case.
switch 1 without fallthru do case 1: ? 1 case 2: ? 2 case 3: ? 3 case 4: ? 4 end switch -- Output: -- 1
Jeremy