Why Switch-Case translate to If/Else?

Monkey Forums/Monkey Programming/Why Switch-Case translate to If/Else?

York(Posted 2011) [#1]
Hey guys,

is there a reason why Monkey translate Switch-Case Conditions to a long If/Else Condition?

A long Switch/Case would be faster than an If/Else-Condition, or?


Tibit(Posted 2011) [#2]
First, that's not an optimization I'd worry to much about. Switch-Statement is more "strict" (some only allow you to use constants) in some languages while if-statemante allow for more dynamic expression logic.

The reason for the conversion I think is that it is a simple way to handle any inconsistency therein, and maybe to also add some usability to monkey :)

However when I decide whether to use switch- or if-statement, I pick the one that gives the best readability.

I think it would be quite difficult to find situations where you could actually motivate replacing a if-statement with a switch due to optimization.


BlackSp1der(Posted 2011) [#3]
I prefer to use switch/case =S


marksibly(Posted 2011) [#4]
Hi,

Mainly because it allows non-constant expressions (and strings!) in cases - in statically typed languages these usually need to be constant.

eg: you can't do this in C++/C#(?)/Java:

Select x$
Case y$
End

I'm pretty sure this'll have no real impact on the efficiency of generated code, except perhaps in 'look up table' situations (or "On Goto" as it used to be called), which are less common in OO languages.

I also hate the default 'fall through' behavior of 'switch' in other languages, and the fact that each case doesn't create its own scope. I also like being able to Exit out of a loop in a Case.

These issues could probably all be solved with some extra code generation to 'special case' const selects etc, but it's way easier just to use if/else instead!


Samah(Posted 2011) [#5]
eg: you can't do this in C++/C#(?)/Java:

Select x$
Case y$
End

I'm pretty sure you could do it in VB6, so they may have brought it across to VB.NET (and therefore C#). I haven't done a lot of C# so I couldn't tell you.

I used to use it back in my noob VB6 days to check selected radio buttons.
Select Case True
  Case radio1.Value
    ' radio1 selected
  Case radio2.Value
    ' radio2 selected
End Select



marksibly(Posted 2011) [#6]
http://msdn.microsoft.com/en-us/library/06tc147t(v=VS.100).aspx

C#'s only allows constants - perhaps VB compiles to Ifs too?


BlackSp1der(Posted 2011) [#7]
I always use switch/case with constants. If I need to compare other values then I can use if/else. I choose which one to use. =)


jalski(Posted 2011) [#8]
I personally think, Limbo style case statement makes the most sense:

	number := 23;
	
	case number {
		0 to 10 =>
			sys->print("number is between 0 - 10\n");
		11 to 20 or 21 to 30 =>
			sys->print("number is between 11 - 30\n");
		* =>
			sys->print("number is between some other range\n");
	}



All case qualifiers must be constants. All case qualifiers and their statements form a separate scope. There is no "fall through" behavior.


xzess(Posted 2011) [#9]
Im always declaring every state for my classes (much more sorted and for each object)

Const OBJECT_LOADED = 0
Const PLAYER_MOVING = 1
Const PLAYER_IDLE = 2
Const PLAYER_CONNECTED = 3
Const LEVEL_FINISHED = 4
Const LEVEL_LOADING = 5
Const LEVEL_PLAYING = 6
Const LOGIC_GAME_WON = 7
Const LOGIC_GAME_LOST = 8
Const LOGIC_GAME_DRAW = 9

Field Status:Int


Then i set the State in the Methods
Method CheckGameStatus()
If(Time = 0)
Self.Status = LEVEL_FINISHED
Else
Self.Status = LEVEL_PLAYING
End
End

And then i do the actions in the OnUpdate() like this:
Select Self.Status
Case PLAYER_CONNECTED
Print "Player connected.."
End


So i really love Switch->Case because its really fast.
But its always best to think about wheither you use Switch-Case or IF-Else
In many ways the best to do is an IF-Clause