Switch vs do case

This forum is meant for questions and discussions about the X# language and tools
ic2
Posts: 1865
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

Switch vs do case

Post 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
User avatar
wriedmann
Posts: 3784
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Switch vs do case

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Frank Maraite
Posts: 178
Joined: Sat Dec 05, 2015 10:44 am
Location: Germany

Switch vs do case

Post 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
User avatar
wriedmann
Posts: 3784
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Switch vs do case

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
ic2
Posts: 1865
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

Switch vs do case

Post 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
Frank Maraite
Posts: 178
Joined: Sat Dec 05, 2015 10:44 am
Location: Germany

Switch vs do case

Post 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
Frank Maraite
Posts: 178
Joined: Sat Dec 05, 2015 10:44 am
Location: Germany

Switch vs do case

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