Select Case - By far the best way!
Using Select Case is by far the very best methodology for implementing well structured decision making procedures. I'm a little uncertain as to just how advantageous it can be in PBP, but in Visual Basic, it's a god sent!
Consider the following:
Code:
SELECT CASE UserInput
CASE 1
GOSUB Temperature
LOW LED
CASE 2
GOSUB LightLevel
HIGH LED
END SELECT
<hr/>
Much easier to read and much faster. Select Case is a much more efficient, IF statement equivalent. However they can at times be a bit quirky for certain things.
<br/>
Slightly more complex Select Case example
Now consider the following:
Code:
Select Case JoystickDat.dwXpos
'//
'right?
'//
Case Is > 50000
If Boat_ID <> 2 Then 'sound on condition
If Master_SND_EN Then
If AllowRowSND = 0 And Jumping = False And Allow_SND_Effects = True Then
SND = sndPlaySound(App.Path & "\Water.wav", CSNDaSync)
AllowRowSND = 1 'sound effect once only
End If '
End If
'// '
Rowing_Forward = True 'set flag
Rowing_Backward = False 'set flag
'// '
If Jumping Then '
'lmitied control when airborne '
PlayerX = PlayerX + (PlayerX < 375) * -1 'inc 1 pixel
Boat_ID = 4 'ors out img
Else '
'maintain screen limitations and inc boat forward
PlayerX = PlayerX + (PlayerX < 250) * -(1 + Row_Speed)
Row_Speed = Row_Speed + (Row_Speed < 20) * -2
Boat_ID = 2 'row img
End If
'// '
Else '
'// '
If Jumping Then '
Boat_ID = 4 'ors in img
Else '
Boat_ID = 1 'ors out img
End If '
End If
'//
'joystick center?
'//
Case Is > 500 And JoystickDat.dwXpos < 50000
If Jumping = False Then
If Master_SND_EN Then
If Row_Speed = 5 And Allow_SND_Effects = True Then
SND = sndPlaySound(App.Path & "\Water B.wav", CSNDaSync)
End If
End If
'//
Row_Speed = Row_Speed + (Row_Speed > 0) * 1 'grad dec speed '
'// '
If Rowing_Forward Then '
Boat_ID = Boat_ID + (Boat_ID = 2) * 1 'reset image
Rowing_Forward = False 'flag
AllowRowSND = 0 'en sound for next time
Else '
Boat_ID = Boat_ID + (Boat_ID = 0) * -1 'reset image
AllowRowSND = 0 'en sound
Rowing_Backward = False 'flag
End If
End If
'//
'left?
'//
Case Is < 500
If Boat_ID <> 0 Then 'sound on condition
If Master_SND_EN Then
If AllowRowSND = 0 And Jumping = False And Allow_SND_Effects = True Then
SND = sndPlaySound(App.Path & "\Water.wav", CSNDaSync)
AllowRowSND = 1 'en sound
End If '
End If
'//
Rowing_Backward = True 'set action flag
Rowing_Forward = False 'set flag
'// '
If Jumping = False Then '
PlayerX = PlayerX + (PlayerX > 50) * 10 'inc 10 pixel backward
Row_Speed = Row_Speed + (Row_Speed > 0) * 2 'grad dec speed '
Boat_ID = 0 'row back img
Else
'lmitied control when airborne '
PlayerX = PlayerX + (PlayerX > 50) * 1 '
Boat_ID = 4 'ors out
End If
'//
Else
'//
If Jumping Then
Boat_ID = 4 'ors out img
Else '
Boat_ID = 1 'ors in img
End If
End If
End Select
'//---//
'Y-axis
'//---//
Select Case JoystickDat.dwYpos
'//
'up?
'//
Case Is < 500
If Jumping Then
PlayerY = PlayerY + (PlayerY > 375) * 2 'inc 2
Else
PlayerY = PlayerY + (PlayerY > 375) * 5 'inc 5
End If
'//
'down
'//
Case Is > 50000
If Jumping Then
PlayerY = PlayerY + (PlayerY < 475) * -2 'inc 2 pixel
Else '
PlayerY = PlayerY + (PlayerY < 475) * -5 'inc 5
End If
End Select
<hr/>
Now, while Select Case itself isn't used for all decision making, its main role here is for structure. It greatly improves clarity and organization. It clearly separates the algorithm into much more easily manageable parts. It's perhaps not quite that obvious in this example because there's really on 2 parts in each Select Case.
<br/>
i knew this was going to happen...
<table><tr><td>http://www.pbpgroup.com/files/dejected.gif</td><td>It wasn't meant to be a 'how to create efficient code' tutorial or contest, but just an example.
Who i'm i anyway ?</td></tr></table>
FYI, in PBP Select Case will be a little less code efficient (talking about code size) than multiple IF-THEN-ELSE... as with ALL other language anyway. Select Case is just easier to read, modify, implement. Save your finger nails...
We are really far of the original question...
Reason why Select Case is faster!
Main reason why Select Case is faster than using multiple IF's is because not all conditions are checked, only the first true condition in a Select Case argument has its underlying block of code executed.
That's my theory on it anyway.
I think there's other diferences too.
Pretty sure you'll find that for every IF argument, unlike Select Case, the subject variable to compare with is loaded into a working register each time.
However with Select Case it's already there for the next compare. Possibly the closed analogy of Select Case would be ELSEIF. But it's no where near as flexible unfortunately.
<br/>