switch confusion

I have two cases in a switch statement that share a lot of code that I don't want to duplicate. In one line they do different things. My intent was to set a flag if the switch variable is L then fall through to the common code. However, when I run this with M the flag variable is still printing the value of 1.

switch ( ch ) {
        case 'S' :   // do something ...
           break;
.....

uint8_t case_L=0;

        case 'L' : 
            case_L=1 ;
        case 'M' : 
            print_uint8_base16(case_L);  printPgmString(PSTR("]\r\n"));   

.....
       break;
}

What am I getting wrong?

I have no idea what your snippet is trying to do. Post the complete code.

It looks like you have uint8_t case_L=0; in no-man's-land between break and the next case

The best way to avoid duplicate code is to put it into a function that can be called from several places.

You may find something useful in planning and implementing a program.

...R

I think you may be over thinking this one. I'm only guessing this is what you're intending (there looks like a better way might exisit).

switch( ch ){
  case 'S' : break;
  
  case 'L' :
  case 'M' :
    print_uint8_base16(ch == 'L');
    printPgmString(PSTR("]\r\n"));
    break;
}

Robin2: I have no idea what your snippet is trying to do. Post the complete code.

It looks like you have uint8_t case_L=0; in no-man's-land between break and the next case

The best way to avoid duplicate code is to put it into a function that can be called from several places.

You may find something useful in planning and implementing a program.

...R

I tired posting but it got refused as too long.

thanks pYro_65, what I posted is obviously trivial and is not what the whole thing is doing. In fact ch is substituting a ref to a char array with a pointer that has moved on by the time I get here. That is why I need to pick up the state by setting a flag variable in case 'L' clause.

Robin. Yes, I could use fns but I'd need at least two and the associated ifs in a structure that is already a chain of elseifs. Setting a flag and testing it once is more efficient and far easier to follow if it worked.

I'm not looking for suggestions of how to code this in a different way, I need to know what I'm getting wrong in the trivialised use of switch.

My understanding was that the L case would only get called if the switch var tested == 'L' and both M and L would fall through to code in the 'M' case block.

However, on first run, and feeding in M it prints that the flag has already been set. :?

You code must be modifying ch somewhere before the switch (got a sneaky - 1 somewhere?)

Put a Serial.print between case L & M. This will prove whether ch is actually still 'M' when it reaches the switch.

Thanks, it seems to be the uint_8 being in no-mans land as Robin pointed out.

It compiled without a warning but seems like it may have been uninitialised in the following case blocks.

I've tidied it up and it now works as I intended.

ardnut:
I tired posting but it got refused as too long.

When that happens post the .ino file as an attachment.

Or, (much better) create a short complete program that illustrates the problem.

…R

Hi,

If your sketch is too big, use REPLY rather than QUICK REPLY and it has an attachment facility, so you can post your sketch as an attachment.

Tom… :slight_smile: