Select Case

regpye

New Member
I have read the manual and seen the given example, but for the love of me I still do not fully understand it.
Can someone please explain how it works and give some examples of it's use?
Thanks
 

Jeff Haas

Senior Member
See post #16 in this thread, for some great example code by Hippy:
 

papaof2

Senior Member
Select Case allows your program to do different things based on the value of a single variable.

Select Case b0 says that byte variable b0 will have different values and the program should do specific actions based on the value of b0.

Case 0 says to do something if b0 = 0
Case 1 says to do something different if b0 = 1

It's the equivalent of a long list of:

If b0 = 0 then do something : endif
If b0 = 1 then do something different ; endif

But there are fewer lines to type, you don't need to remember ENDIF on each line and it probably runs faster.

Does that help?
 

Aries

New Member
It's really just another way of doing repeated "if...then" statements
Code:
if b0 = 1 then
gosub Sub1
goto EndOfList
endif
if b0 = 2 then
gosub Sub2
goto EndOfList
endif
if b0 >= 3 and b0 <= 6 then
gosub Sub3
goto EndOfList
endif
EndOfList:
can be written as
Code:
select case b0
case 1
  gosub Sub1
case 2
  gosub Sub2
case 3 to 6
  gosub Sub3
end select
 
Last edited:

neiltechspec

Senior Member
An example for you, extract from my Classic Car Cooling Fan controller.

Code:
readtemp sens,b1
select case b1    'set pwm according to sensor temperature, PWM % is fan type dependant
     case >= 127 goto frost
     case >= 81 pwmout pwmdiv4,FET,124,495:high green:goto main '99%
     case >= 80 pwmout pwmdiv4,FET,124,490:high green:goto main
     case >= 79 pwmout pwmdiv4,FET,124,485:high green:goto main
     case >= 78 pwmout pwmdiv4,FET,124,480:high green:goto main
     case >= 77 pwmout pwmdiv4,FET,124,475:high green:goto main
     case >= 76 pwmout pwmdiv4,FET,124,450:high green:goto main
     case >= 75 pwmout pwmdiv4,FET,124,437:high green:goto main
     case >= 74 pwmout pwmdiv4,FET,124,425:high green:goto main
     case >= 73 pwmout pwmdiv4,FET,124,412:high green:goto main
     case >= 72 pwmout pwmdiv4,FET,124,410:high green:goto main
     case >= 71 pwmout pwmdiv4,FET,124,400:high green:goto main
     case >= 70 pwmout pwmdiv4,FET,124,375:high green:goto main
     case >= 69 pwmout pwmdiv4,FET,124,362:high green:goto main
     case >= 68 pwmout pwmdiv4,FET,124,350:high green:goto main
     case >= 67 pwmout pwmdiv4,FET,124,337:high green:goto main
     case >= 66 pwmout pwmdiv4,FET,124,325:high green:goto main
     case >= 65 pwmout pwmdiv4,FET,124,312:high green:goto main
     case >= 64 pwmout pwmdiv4,FET,124,300:high green:goto main
     case >= 63 pwmout pwmdiv4,FET,124,287:high green:goto main
     case >= 62 pwmout pwmdiv4,FET,124,275:high green:goto main
     case >= 61 pwmout pwmdiv4,FET,124,267:high green:goto main
     case >= 60 pwmout pwmdiv4,FET,124,250:high green:goto main
     case >= 59 pwmout pwmdiv4,FET,124,225:high green:goto main
     case >= 58 pwmout pwmdiv4,FET,124,210:high green:goto main
     case >= 57 pwmout pwmdiv4,FET,124,200:high green:goto main
     case >= 56 pwmout pwmdiv4,FET,124,195:high green:goto main
     case >= 55 pwmout pwmdiv4,FET,124,190:high green:goto main
     case >= 54 pwmout pwmdiv4,FET,124,185:high green:goto main
     case >= 52 pwmout pwmdiv4,FET,124,180:high green:goto main
     case >= 36 pwmout pwmdiv4,FET,124,175:high green:goto main '35%
     case <= 35 pwmout FET,off:low green:goto main
    endselect
 

lbenson

Senior Member
Code:
readtemp sens,b1
select case b1    'set pwm according to sensor temperature, PWM % is fan type dependant
     case >= 127 goto frost
     case >= 81 pwmout pwmdiv4,FET,124,495:high green:goto main '99%
...
    endselect
Instead of all those "goto main"s, why not omit them and close with

Code:
    else goto wherever ' if necessary
  endselect
  goto main
 

kfjl

Member
@regpye:

If you don't understand post #8, don't worry, it's an awful example.
If you don't understand the second part of post #5, you can worry.
 

hippy

Technical Support
Staff member
If you don't understand post #8, don't worry, it's an awful example.
I think the phrase you are looking for "could have been written differently", "could be improved upon", and I would agree but, as the author says, "Why bother, it works fine & has done for years. If it aint broke, don't fix it !!!" and that's an absolutely fine and reasonable response.

In illustrating the 'if this then that' nature of a SELECT-CASE it does its job.
 

hippy

Technical Support
Staff member
I had a look there and I still don't fully understand how it works.
One of the best ways to get to understand how particular commands work is to create a test program and run it in the Simulator, see what it does. For example -
Code:
SerTxd( "Starting", CR, LF )
For b0 = 0 To 5
  SerTxd( #b0 )
  Select Case b0
    Case 1 : SerTxd( " - The number is one"  )
    Case 2 : SerTxd( " - The number is two"  )
    Case 4 : SerTxd( " - The number is four" )
  End Select
  SerTxd( CR, LF )  
Next
SerTxd( "Finished", CR, LF )
That will show the following in the Terminal Window when simulated -
Code:
Starting
0
1 - The number is one
2 - The number is two
3
4 - The number is four
5
Finished
You can then modify that to test for other combinations of SELECT-CASE use, for example -
Code:
  Select Case b0
    Case >= 4 : SerTxd( " - The number is greater or equal to 4" )
    Case >= 2 : SerTxd( " - The number is greater or equal to 2" )
    Case >= 1 : SerTxd( " - The number is greater or equal to 1" )
  End Select
You can also try the following to see why it must be as above to work how that does -
Code:
  Select Case b0
    Case >= 1 : SerTxd( " - The number is greater or equal to 1" )
    Case >= 2 : SerTxd( " - The number is greater or equal to 2" )
    Case >= 4 : SerTxd( " - The number is greater or equal to 4" )
  End Select
 

regpye

New Member
One of the best ways to get to understand how particular commands work is to create a test program and run it in the Simulator, see what it does. For example
Thanks Hippy, that's what I was looking for, it explains it so I can understand now, thanks.
So I put it together and tested it like this;
Code:
SerTxd( "Starting", CR, LF )
For b0 = 0 To 5
  SerTxd( #b0 )
  Select Case b0
    Case 1 : SerTxd( " - The number is one"  )
    Case 2 : SerTxd( " - The number is two"  )
    Case 4 : SerTxd( " - The number is four" )
  End Select
  SerTxd( CR, LF )  
Next
SerTxd( "Finished", CR, LF )
For b1 = 0 To 3
Select Case b1
    Case >= 4 : SerTxd( " - The number is greater or equal to 4" )
    Case >= 2 : SerTxd( " - The number is greater or equal to 2" )
    Case >= 1 : SerTxd( " - The number is greater or equal to 1" )
  End Select
SerTxd( CR, LF )
Next
SerTxd( "Finished 2nd test", CR, LF )
and I got these results.

Starting
0
1 - The number is one
2 - The number is two
3
4 - The number is four
5
Finished

- The number is greater or equal to 1
- The number is greater or equal to 2
- The number is greater or equal to 2
Finished 2nd test

I am wondering why I got two 2's and not a 4 ?
 
Last edited:

Buzby

Senior Member
Modify your second test to show the value of b1 at each step ...

Select Case b1
Case >= 4 : SerTxd( #b1," - The number is greater or equal to 4" )
Case >= 2 : SerTxd( #b1," - The number is greater or equal to 2" )
Case >= 1 : SerTxd( #b1," - The number is greater or equal to 1" )
End Select

Then you will see why !
 

kfjl

Member
I think the phrase you are looking for "could have been written differently", "could be improved upon", and I would agree but, as the author says, "Why bother, it works fine & has done for years. If it aint broke, don't fix it !!!" and that's an absolutely fine and reasonable response.

In illustrating the 'if this then that' nature of a SELECT-CASE it does its job.
No, that's not the phrase I was looking for. I wasn't talking about the code, I was talking about the example.
The example in post #5 is short and simple and is a good example.
neiltechspec's logorrhea is not.
 

regpye

New Member
Your second loop is from b1 = 0 to 3, so it never reaches 4
Okay, I changed it to 4 loops and I get this, which is a bit different to the first test above.
The first test shows some missed lines which I can understand, but the second test has an extra line for the 2.

Starting
0
1 - The number is one
2 - The number is two
3
4 - The number is four
5
Finished

- The number is greater or equal to 1
- The number is greater or equal to 2
- The number is greater or equal to 2
- The number is greater or equal to 4
Finished 2nd test

I took the >= out and tried again making it the same as the first test and I get the results the same as the first test, so there is something to learn about the second test.

Starting
0
1 - The number is one
2 - The number is two
3
4 - The number is four
5
Finished

- The number is greater or equal to 1
- The number is greater or equal to 2

- The number is greater or equal to 4
Finished 2nd test
 
Last edited:

regpye

New Member
I am now getting more questions about the code. I changed a few things to give some different examples and it has opened up another set of questions.
The results from the code added at the bottom of this post came out like this;
Starting
0
1 - The number is one
2 - The number is two
3
4 - The number is four
5
Finished 1st test OK I understand this test
--------------------------
Now using >=

- The number is greater or equal to 1
- The number is greater or equal to 2
- The number is greater or equal to 2 I don't understand why this appears
- The number is greater or equal to 4
Finished 2nd test
--------------------------
Now using <=
- The number is less than or equal to 4
- The number is less than or equal to 4
- The number is less than or equal to 4
- The number is less than or equal to 4
- The number is less than or equal to 4 Only four loops but five answers???
Finished 3rd test

Code:
; *******************************
;    Written by:
;    Function:  Testing SELECT CASE      
;    Target PICAXE:   any 
; *******************************

SerTxd( "Starting", CR, LF )
For b0 = 0 To 5
  SerTxd( #b0 )
  Select Case b0
    Case 1 : SerTxd( " - The number is one"  )
    Case 2 : SerTxd( " - The number is two"  )
    Case 4 : SerTxd( " - The number is four" )
  End Select
  SerTxd( CR, LF )  
Next
SerTxd( "Finished 1st test", CR, LF )
SerTxd( "--------------------------", CR, LF )

SerTxd( "Now using >=", CR, LF )
For b1 = 0 To 4
Select Case b1
    Case >= 4 : SerTxd( " - The number is greater or equal to 4" )
    Case >= 2 : SerTxd( " - The number is greater or equal to 2" )
    Case >= 1 : SerTxd( " - The number is greater or equal to 1" )
  End Select
SerTxd( CR, LF )
Next
SerTxd( "Finished 2nd test", CR, LF )
SerTxd( "--------------------------", CR, LF )

SerTxd( "Now using <=", CR, LF )
For b2 = 0 To 4
Select Case b2
    Case <= 4 : SerTxd( " - The number is less than or equal to 4" )
    Case <= 2 : SerTxd( " - The number is less than or equal to 2" )
    Case <= 1 : SerTxd( " - The number is less than or equal to 1" )
  End Select
SerTxd( CR, LF )
Next
SerTxd( "Finished 3rd test", CR, LF )
SerTxd( "--------------------------", CR, LF )
 

Buzby

Senior Member
Modify the test to show the value of the variable at each step ...

Select Case b1
Case >= 4 : SerTxd( #b1," - The number is greater or equal to 4" )
Case >= 2 : SerTxd( #b1," - The number is greater or equal to 2" )
Case >= 1 : SerTxd( #b1," - The number is greater or equal to 1" )
End Select

Then you will see why !
 

regpye

New Member
Modify the test to show the value of the variable at each step ...
Thanks Buzby,
I did that and also in test 3 area, I see what is happening but don't know why.

Starting
0
1 - The number is one
2 - The number is two
3
4 - The number is four
5
Finished 1st test
--------------------------
Now using >=
zero missing here
1 - The number is greater or equal to 1
2 - The number is greater or equal to 2
3 - The number is greater or equal to 2 should this be displayed??
4 - The number is greater or equal to 4
Finished 2nd test
--------------------------
Now using <=
0 - The number is less than or equal to 4
1 - The number is less than or equal to 4
2 - The number is less than or equal to 4
3 - The number is less than or equal to 4
4 - The number is less than or equal to 4 why 5 replies??
Finished 3rd test
--------------------------
 

Aries

New Member
Think about what you are testing ...
In the second test, you do not have a case which a value b1=0 will pass, hence there is no zero
The value b1=3 IS >=2, so it reports accordingly.

In the third test, your loop is b1 = 0 to 4, which is 5 values. Hence, when you have a set of cases which b1 will pass for any value, you get five results.
 

Buzby

Senior Member
'Zero missing' in the second loop is as Aries says, no test that 0 will match.

However, the first loop also has tests that don't match, on 0, 3, and 5, yet in these cases a blank result is given.

Can you see why the two loops differ in the way non-matching results are displayed ?.

( Hint - Look at the lines that print the variable's value. )
 

regpye

New Member
The value b1=3 IS >=2, so it reports accordingly.

In the third test, your loop is b1 = 0 to 4, which is 5 values. Hence, when you have a set of cases which b1 will pass for any value, you get five results.
Thank Aries, I understand now, just need to do it a few times to let it sink in.
 
Top