Select/Case observation- it is "short circuiting"

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
Gordon_h
Posts: 55
Joined: Fri Apr 06, 2007 8:55 pm
Location: Boulder, Colorado

Select/Case observation- it is "short circuiting"

Post by Gordon_h » Mon Jul 08, 2013 7:51 pm

I was wanting to use a Select/Case structure in a different way, such that somethings were done for individual cases, and some for combinations. I tried the following:

Code: Select all

for i=0 to 2
  mc_isrtxrx.write("i: "+dectostr(i)+" ")
  select i
    case 0
      mc_isrtxrx.write("Case 0"+cr+lf)
    case 1
      mc_isrtxrx.write("Case 1"+cr+lf)
     case 0, 1
      mc_isrtxrx.write("Case 0,1"+cr+lf)
    case 2
      mc_isrtxrx.write("Case 2"+cr+lf)
    case 0,1,2
      mc_isrtxrx.write("Case 0,1,2"+cr+lf)
  end select
next

which uses my interrupt driven "Write" routine.

The result is:

i: 0 Case 0
i: 1 Case 1
i: 2 Case 2

I guess I should have expected this "short circuiting" behavior, most languages do this. I just did not read carefully enough the line in the help file:

"If one of the case conditions is met, then any statements following the condition are executed and the program jumps to any code immediately following endselect"

So in this case I will have to use a series of (non-nested) IF/THEN statements. Just not quite as elegant-looking!

Maybe this will help someone else not waste any time !

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Mon Jul 08, 2013 8:13 pm

What's a "combination" vs an "individual"?

I'd be interested in seeing how you differentiate between a value being 0, 1, or 2 and (0 or 1 or 2)

User avatar
RadioT
Registered User
Registered User
Posts: 157
Joined: Tue Nov 27, 2007 12:50 pm
Location: Winnipeg, Canada

Post by RadioT » Tue Jul 30, 2013 5:50 pm

You are correct. The one statement is executed, then it leaves the case when that one statement is completed.

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Tue Jul 30, 2013 7:57 pm

My point was that even if you rewrite the SELECT using IF/THEN statements, there's no way to distinguish between some of those conditions unless you want it to print out multiple responses (and maybe he does).

For example, assume SELECT didn't short-circuit. If i = 1 then you'd get:

Case 1
Case 0,1
Case 0,1,2

since they are all true statements. Rewriting it using IF/THEN doesn't change that.

Post Reply