Switch
Moderator: Klaus
-
- VIP Livecode Opensource Backer
- Posts: 977
- Joined: Sat Apr 08, 2006 7:47 am
- Contact:
That's comparing apples and oranges, I'm afraid:
- the actual script block is: 'switch', 'end switch'
- and you may not put 'break' there when you want to let it fall through to the next case
Jan Schenkel.
- the actual script block is: 'switch', 'end switch'
- and you may not put 'break' there when you want to let it fall through to the next case
Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com
www.quartam.com
History
The issue you're going to run into is that you're dealing with history. RR is the successor of several legendary products that started 20 years ago, and inherited the language and the paradigm from them.
the two-word "end if" and "end repeat" are part of that rich heritage. The language is by nature verbose, as it is designed to be easier to read, not easier to type.
the two-word "end if" and "end repeat" are part of that rich heritage. The language is by nature verbose, as it is designed to be easier to read, not easier to type.
Hi,
Jan is right. It doesn't make sense to compare "case" with "if". At all. The switch-equivalent of "if" is.... "switch"!
Why is it different? Simple... because it is way faster!
The if-then-else control structure requires the engine to check for each condition separetely, while with each check the condition is parsed again. If the switch control structure is used correctly, the engine needs to parse the condition only once and only needs to check one single value after parsing the condition.
Example:
The if statement is a lot more work for the engine, because the engine doesn't know when to jump to the end-if statement, while in the switch control structure it is clear that if one or more cases apply, the engine can skip the remaining part of the switch control structure and jump to the end-switch statement immediately.
I hope I have made clear that there is a good reason for things to work the way they do.
Best,
Mark
Jan is right. It doesn't make sense to compare "case" with "if". At all. The switch-equivalent of "if" is.... "switch"!
Why is it different? Simple... because it is way faster!
The if-then-else control structure requires the engine to check for each condition separetely, while with each check the condition is parsed again. If the switch control structure is used correctly, the engine needs to parse the condition only once and only needs to check one single value after parsing the condition.
Example:
Code: Select all
if fruit is "orange" or fruit is "apple" then -- parse and check twice
-- do something here and go on checking the next
-- if statement
else if fruit is "pear" then -- parse and check again
-- do something else
else
-- do something else again
end if
switch fruit -- parse and store the value in memory
case "orange" -- just check value
case "apple" -- just check value
-- do something
break -- if above applies, don't check next values but
-- jump to end switch immediately
case "pear'
-- do something
break -- if above applies, jump
default -- none of the above applies
-- do something else
end switch
I hope I have made clear that there is a good reason for things to work the way they do.
Best,
Mark
Last edited by Mark on Sat Aug 09, 2008 10:43 am, edited 1 time in total.
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
Hi,
Reconsidering what I wrote above, I think that the examples miss a point :-) They are good for showing a difference in the way the conditions are parsed, but not for explaining that part of the if-then-else control structure is executed while the equivalent part of the switch control structure is not executed. Here's another example, a bit simpler.
In above example, every if/else statement is parsed, except the last one. First "if false", then "if true". The "else" statement is ignored. If you use the debugger, you will notice that the engine executes all these lines.
Now look at the next example.
In the above, the engine will immediately jump to "case true" and ignore the "case false" above it. So, the engine immediately executes the condition that is true and ignores everything else. Again, you can check this in the debugger.
My explanation might still be a candidate for improvement, but I hope this makes it more clear.
Best,
Mark
Reconsidering what I wrote above, I think that the examples miss a point :-) They are good for showing a difference in the way the conditions are parsed, but not for explaining that part of the if-then-else control structure is executed while the equivalent part of the switch control structure is not executed. Here's another example, a bit simpler.
Code: Select all
on mouseUp
if false then
beep
else if true then
beep
else
beep
end if
end mouseUp
Now look at the next example.
Code: Select all
on mouseUp
put true into x
switch x
case false
beep
break
case true
beep
break
end switch
end mouseUp
My explanation might still be a candidate for improvement, but I hope this makes it more clear.
Best,
Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
-
- Posts: 179
- Joined: Sat Apr 08, 2006 11:08 pm
- Contact:
Re: History
It's not just about history. The switch structure is not unique to revolution at all - it's quite a common form.bjb007 wrote: History is a poor excuse I think.
Ease of use is of much greater importance.
As Jan pointed out, the script block is switch-end switch, and is not directly equivalent to if structures.
case-end case would be a bit like having to have else-end else within an if structure.
Best,
Mark