Switch vs do case
Posted: Mon Sep 18, 2017 11:44 am
Hello Frank,
The first time I used Switch in C# I didn't realize it continued after a matching condition was found. You have to add code to leave the statement for every condition. I found it another example of VO being superior in language design to C#.
You explanation made it clear it does have advantages. If Switch were precompiled AND leaving the loop it would be an improvement. Now I have to choose between adding extra code to leave the statements (I dislike extra code) but a check on invalid values (+ extra speed but I never have more than a handful of conditions so that won't count) - or the good old DO..CASE which allows me to make mistakes.
Dick
A very interesting and clear explanation you gave here.yes, VO was leaving when a match was found. But if you have a thousend CASE's und the last one matches, it did 999 comparisons/test first. The same with X# of course. In the case of sDeveloper:ToUpper() it will execute this method call 999! times for nothing.
SWITCH does one test and then knows what to do.
But switch does more: it checks at compile time if there are to identical conditions.
The first time I used Switch in C# I didn't realize it continued after a matching condition was found. You have to add code to leave the statement for every condition. I found it another example of VO being superior in language design to C#.
You explanation made it clear it does have advantages. If Switch were precompiled AND leaving the loop it would be an improvement. Now I have to choose between adding extra code to leave the statements (I dislike extra code) but a check on invalid values (+ extra speed but I never have more than a handful of conditions so that won't count) - or the good old DO..CASE which allows me to make mistakes.
Dick