Page 1 of 1

PostPosted: Mon Apr 28, 2003 3:12 pm
by cable_guy_67
Hello all,
I have been working on something that works perfectly for a:
case "01":

But I am not accessesing all my cases. Can you nest switch() statements as long as you place the } after the default: ?

Example code:

//comming from a switch()
case "01":
case "60":
case "61":
alert("Pick a location to move that to.");
if ((placex==4)&&(placey==0)){
case "10":
alert("You have chosen to move the male tray to the operating theater.");

And it goes on to close all open statements.
I just want to be sure this is legal and find my problem elsewhere in the code. I have an old C manual and it doesn't say one way or the other.


PostPosted: Tue Apr 29, 2003 12:42 am
by jstadolnik
Nested switches should work (at least they're supposed to).

Try doing a StrLen() before on string before you pass it to the switch. You may have a stray whitespace character in the the string?


The PToolboxLib guy.

PostPosted: Tue Apr 29, 2003 10:39 am
by cable_guy_67
I think you are right about checking the variable for the switch. Realistically, how many case: statements is too much? I have 80 different ones (main case:'s and nested cases:'s) and I can see the lag on the actual palm. It runs great on the pose but runs slowly on my IIIc. Would it be better to break them down into 4 sections and direct accordingly to increase speed. As I am totally new to Pocket C (and C for that matter) I have a lot of deep if/else loops and calling a function from a function. Thanks for the info Joe. And for any newcomers I have some book recommendations.

1: Palm Programming for the Absolute Beginner.
. I know this has been described a lot here but it is invaluable in really getting Pocket C. A bit old but you can get it for around $20 on amazon.
2: A Step-by-Step Guide to C Programming by Jean Paul Corriveau.
3: Complete Book of C Programming by Barry M Austell-Wolfson and R Derek Otieno.
. Of these two I like the first better. They are from 1999 and 2000 and written like textbooks with suggestions and examples. One of these books will help you when all other documentation fails! I had them sent to me from our wonderful Public Library System. They have lots of books that rarely get requested.

After all, I went from total maroon to having a competent grasp of the language with 2 hard weeks, you can too.
Just my two cents. :D
And Congrats Joe, you sounded pumped re:new version of PToolBox.


PostPosted: Tue Apr 29, 2003 12:06 pm
by jstadolnik
Try using integers as the switch variables, In general they work faster than strings, and this may allow the compiler to make further optimizations.

Also, try breaking up your switch structures so that they each have fewer case statements.

One more... check out the speedopts.c file in the PToolbox release. Specifically item #11. It shows a technique that's even faster than a switch-case.


The PToolboxLib guy.

PostPosted: Tue Apr 29, 2003 2:34 pm
by wolfgang
Probably the fastest substitute for long switch clauses or eternal if/elseif lists: If the possible range of values is limited (e.g. let's say variable myvar can only hold values from 1...10, then the fastest is to have an array of function pointers and call the respective functions. Each function contains the code from one case clause of your switch statement.

// define the functions with the necessary code

myfunc1() {....}

myfunc2() {...}

... and so on

main ()
int myvar;
pointer myarr[100];

myarr[1] = myfunc1; // pointer to first function
myarr[2] = myfunc1; // pointer to second function
...and so on

... now determine the switch variable somehow....

// here comes the switch replacement - this calls the proper
// function based on what value myvar has. If myvar == 1, it calls
// myfunc1(), if myvar is 2, then it calls myfunc2(), and so on

(*(myarr [myvar])) (); // that's all!!!

This is blazingly fast, independent of the number of different cases.


Composing music on the PocketPC! -

PostPosted: Tue Apr 29, 2003 4:50 pm
by cable_guy_67
I haved saved this thread on my local machine and will use those suggestions. Thanks Joe and Wolfgang for the input, it answered all my questions. Wolfgang, your tutorials were the first I learned to code in Pocket C with. I like the simple explanations.

Off to implement &
read more PToolBox least the're not terse. :D