xsharp.eu • Switch vs do case - Page 3
Page 3 of 3

Switch vs do case

Posted: Mon Sep 18, 2017 11:44 am
by ic2
Hello Frank,
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.
A very interesting and clear explanation you gave here.
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

Switch vs do case

Posted: Mon Sep 18, 2017 11:49 am
by wriedmann
Hi Dick,

the switch is different between X# and C#.

This is the code from the relative X# sample:

Code: Select all

// The SWITCH statement is a replacement for the DO CASE statement
// The biggest difference is that the expression (in this case sDeveloper) is only evaluated once.
// which will have a performance benefit over the DO CASE statement
//
using System.Collections.Generic

Function Start() as void
    FOREACH VAR sDeveloper in GetDevelopers()
	SWITCH sDeveloper:ToUpper()
	CASE "FABRICE"
		? sDeveloper, "France"
	CASE "CHRIS"
	CASE "NIKOS"
		? sDeveloper, "Greece"
	CASE "ROBERT"
		? sDeveloper, "The Netherlands"
	OTHERWISE
		? sDeveloper, "Earth"
	END SWITCH
   NEXT
Console.ReadKey()
RETURN
As you can see, you don't need to break out in X#.

Wolfgang

Switch vs do case

Posted: Mon Sep 18, 2017 11:59 am
by Frank Maraite
Dick, Wolfgang, Robert,

seemes this is true only for C#6 compatible switches. Look here

(german)
https://docs.microsoft.com/de-de/dotnet ... rds/switch

english
https://docs.microsoft.com/en-us/dotnet ... rds/switch

In C#7 they made extensions to switch. Don't how how the devteam will manage this in the future.

Frank

Switch vs do case

Posted: Mon Sep 18, 2017 12:21 pm
by wriedmann
Hi Frank,

personally I think we have "switch" and "do case" - the C# people does not have our "do case", and the extended switch format seems to be very similar to the xBase do case format.

IMHO we have more urgent things than language extensions at this moment, the runtime and the RDDs for example.

The X# language itself is very complete, and I like it very much to work with it. One of my favorites is the short property syntax.

Wolfgang

Switch vs do case

Posted: Mon Sep 18, 2017 12:59 pm
by ic2
As you can see, you don't need to break out in X#.
Hello Wolfgang, Frank,

That's great - I made a note for it. So the X# Switch statement works as the C# should have been.

Not sure Frank what you meant with the link. It says that you need 'break' in every statement. Which is not needed in X# and that's a lot more logical and a lot less code, especially in your 999 statements large switch statements.

Dick

Switch vs do case

Posted: Mon Sep 18, 2017 1:26 pm
by Frank Maraite
Dick,

just read it!

They say that until C#6 they have only constant case's. With C#7 there are other types allowed. But what I said: just read it, then you know.

Frank

Switch vs do case

Posted: Mon Sep 18, 2017 2:23 pm
by Frank Maraite
Dick,

just read it!

They say that until C#6 they have only constant case's. With C#7 there are other types allowed. But what I said: just read it, then you know.

Frank