Page 1 of 1

Switch

Posted: Tue Jul 15, 2008 2:03 am
by bjb007
We have

if, end if
repeat, end repeat

but for some reason we have

case,break

For consistency perhaps

case, end case?

Even better endif,endrepeat,endcase

Posted: Tue Jul 15, 2008 6:03 am
by Janschenkel
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.

History

Posted: Mon Jul 21, 2008 3:53 pm
by Mikey
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.

History

Posted: Sat Aug 09, 2008 7:51 am
by bjb007
Well Mikey the world of commerce is
knee-deep in dead corporations which
didn't think they should move with the
times.

History is a poor excuse I think.

Ease of use is of much greater importance.

Posted: Sat Aug 09, 2008 10:08 am
by Mark
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:

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
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

Posted: Sat Aug 09, 2008 10:37 am
by Mark
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.

Code: Select all

on mouseUp
   if false then
      beep
   else if true then
      beep
   else
      beep
   end if
end mouseUp
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.

Code: Select all

on mouseUp
   put true into x
   switch x
      case false
         beep
      break
      case true
         beep
      break
   end switch
end mouseUp
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

Re: History

Posted: Sat Aug 09, 2008 7:46 pm
by Mark Smith
bjb007 wrote: History is a poor excuse I think.

Ease of use is of much greater importance.
It's not just about history. The switch structure is not unique to revolution at all - it's quite a common form.

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