What does CASE actually mean?

Thread Starter

camerart

Joined Feb 25, 2013
1,969
Hi,
I'm programming (Well mostly someone else is programming) PICs and I keep coming across CASE
I can understand English words like FOR and NEXT but when it comes to CASE, it doesn't sink in.

Can someone describe it in a simple way, so that I can understand and use it please?
Camerart.
 

ericgibbs

Joined Jan 29, 2010
10,005
hi C,
Select Case X
Case 0
do this one
'exit the Case test
Case 1
do this one
'exit the Case test
End Select

Its a simple way of avoiding writing multiple IF statements.

So if X = 0 do this
if X = 1 do this
if X= 2 do this one

OK.???
E
 

Thread Starter

camerart

Joined Feb 25, 2013
1,969
hi C,
Select Case X
Case 0
do this one
'exit the Case test
Case 1
do this one
'exit the Case test
End Select

Its a simple way of avoiding writing multiple IF statements.

So if X = 0 do this
if X = 1 do this
if X= 2 do this one

OK.???
E
Hi E,
Not really! I have this type of thing in my programs, perhaps written by you, but to me it reads like a foreign language.
If you were writing a Programming dictionary, how would you describe CASE, without examples?
C
 

ericgibbs

Joined Jan 29, 2010
10,005
Hi C,

if the value = this value then do this code
if the value = this value then do this code
if the value = this value then do this code

You are over thinking the problem, if the value matches one of the values in the Case list, do that Code and don't test any others in the Case List, until the next time you visit the Case list
E
 

Thread Starter

camerart

Joined Feb 25, 2013
1,969
Hi C,

if the value = this value then do this code
if the value = this value then do this code
if the value = this value then do this code

You are over thinking the problem, if the value matches one of the values in the Case list, do that Code and don't test any others in the Case List, until the next time you visit the Case list
E
Hi E,
True I am thinking alot about it, and I need to understand it, to sort out the problem in my programs, but at the moment It's baffling. EDIT: I have to understand something to use it
Thanks.
C
 
Last edited:

ericgibbs

Joined Jan 29, 2010
10,005
hi,
Consider a Street that has say 10 houses numbered from 1 to 10.
I give you a letter to post in house #3, you walk past houses 1 and 2 on the street until you come to #3, you post the letter [ or do the Code for #3]
Next time i see you I give you a letter for house number #7, you walk past houses 1 to 6 until you come to house #7, you post the letter [ or do the Code for #7]
 

Thread Starter

camerart

Joined Feb 25, 2013
1,969
As an approximation, think "circumstance" when you see 'CASE'.
Hi A,
In 'E's' last message this example [ if the value = this value then do this code ] I understand ok, but when I see 'CASE 0' for example, What does CASE mean and why 0. What's the difference between CASE 0 and CASE 1? At the bottom of the CODE I'm trying to understand, there is CASE ELSE, again, what does that mean in English?

I note that there is always a number e,g, 0 or 1 or 5. Are these binary, how big number can be used.

Circumstance. Is it similar to, 'in the first CASE', 'in the second CASE'? (First being 0)
C.
 

Thread Starter

camerart

Joined Feb 25, 2013
1,969
hi,
Consider a Street that has say 10 houses numbered from 1 to 10.
I give you a letter to post in house #3, you walk past houses 1 and 2 on the street until you come to #3, you post the letter [ or do the Code for #3]
Next time i see you I give you a letter for house number #7, you walk past houses 1 to 6 until you come to house #7, you post the letter [ or do the Code for #7]
Hi E,
Good example, I'll peruse it, thanks.
C
 

Thread Starter

camerart

Joined Feb 25, 2013
1,969
Hi E,
Good example, I'll peruse it, thanks.
C
Hi again E,
While perusing, I came across this Oshonsoft example:

I've commented it using how I see your 'house' example.
C.
-------------------------------------------
Dim x As Byte
loop:
Select Case x
Case 255 (At house 255 X=1)
x = 1
Case <= 127 (At houses 0 to 127 X=X+1)
x = x + 1
Case Else
x = 255 (At houses 128 to 254 X=255) (Assuming the houses stop at 255)
EndSelect
Goto loop
---------------------------------------------------
 

ericgibbs

Joined Jan 29, 2010
10,005
hi,
Case Else
Means if there is no match do this Code.
If I gave you a letter for house #11, no such house number so do this, >> take it back to the office [ Code]
 

MrChips

Joined Oct 2, 2009
21,051
The meaning of "case" in an English dictionary is: instance, occurrence, situation, existence.

Hence in a programming context, if the CASE of something occurs, then execute the following instructions.
ELSE would mean if no previously declared cases occurred, execute the following instructions.
 

djsfantasi

Joined Apr 11, 2010
6,407
For the following CASEs of this variable, execute the associated block of code.

CASE x // variable equals x​

Code:
int variable = 1;

select  case variable;

  case 0;
  // this block of code is executed 
  // if it’s the case that variable=0
  case 1;
  // this block of code is executed 
  // if it’s the case that variable=1
 

Thread Starter

camerart

Joined Feb 25, 2013
1,969
The meaning of "case" in an English dictionary is: instance, occurrence, situation, existence.

Hence in a programming context, if the CASE of something occurs, then execute the following instructions.
ELSE would mean if no previously declared cases occurred, execute the following instructions.
Hi Mr C,
So in #10 example is this correct:
Thanks, C.

-------------------------------------------------
loop:
Select Case x
Case 255 (At house 255 X=1)
x = 1
Case <= 127 (At houses 0 to 127 X=X+1)
x = x + 1
Case Else
x = 255 (If CASE 255 didn't occure and CASE <= 127 didn't occure then x=255)?
EndSelect
Goto loop
----------------------------------------------------
 

Thread Starter

camerart

Joined Feb 25, 2013
1,969
For the following CASEs of this variable, execute the associated block of code.

CASE x // variable equals x​

Code:
int variable = 1;

select  case variable;

  case 0;
  // this block of code is executed
  // if it’s the case that variable=0
  case 1;
  // this block of code is executed
  // if it’s the case that variable=1
Hi D,
The first mention of a VARIABLE!
Helpful, thanks.
C
 
Top