Shop OBEX P1 Docs P2 Docs Learn Events
Coming from PBASIC to Prop C: Select Case / switch — Parallax Forums

Coming from PBASIC to Prop C: Select Case / switch

John KauffmanJohn Kauffman Posts: 653
edited 2014-02-18 18:33 in Propeller 1
Does this seem right? It works but is there a better technique?

PBASIC
[/code]
myDiscountCode VAR Nib
myPrice VAR Byte
myDiscountCode = 1

SELECT myDiscountCode
CASE 1 'member discount
myPrice = 6
CASE 2,3 'senior and student
myPrice = 8
CASE 4 TO 7 'local resident
myPrice = 9
CASE ELSE 'full price
myPrice = 10
ENDSELECT
DEBUG myPrice
[/code]

Prop C
Use Switch instead of Select Case. All the cases go in one {} of the Switch.
Each case must end with a break (One case’s code does not automatic stop when next case starts; it will run through.)
Case lines end in colon. As usual, semicolon at end of each line of code within the Case
default instead of ENDSELECT
For a range, there is no “TO” so change from switch to if()

[/code]
int myDiscountCode;
int myPrice;
myDiscountCode = 7; // hard code, set before run

switch(myDiscountCode) {
case 1: //member discount
myPrice = 6;
break;
case 2: //n.b. no break so flows to case 3
case 3: //n.b. covers student and senior
myPrice = 8;
break;
// no "TO" syntax. Use listing (n.b. no commas) or if()
case 4: case 5: case 6: case 7:
myPrice = 9;
break;
default: //full price
myPrice = 10;
break;
} //switch
print("myDiscountCode = %d.\nmyPrice = %d.",myDiscountCode,myPrice);
[/code]
Sign In or Register to comment.