maybe I'm just being dumb but I'm not getting expected output from a switch statement.
switch( line[char_counter] ) { // leave visible trace of input
case 'S' : {printString(line);serial_write('S');}
case 'L' : {printString(line);serial_write('L');}
// case 'Z' : printString(line);
}
The char pointed to by line[char_counter] can only have one value at the time this is executed but both cases are printing. if I add the Z case I get three lines printed .
I could add a break ( which would be more efficient) but this should not be necessary.
I found a bug relating to switch on AVR dating back to 2002 but nothing about this. What am I missing?
[BTW how to post code snippets here, posting as a quotation is not the best]
ardnut:
[BTW how to post code snippets here, posting as a quotation is not the best]
Use the </> icon above the and it comes out...
like this
But, snippets as opposed to whole sketches are generally frowned upon, since often the problem is actually somewhere else in the sketch, not the snippet.
I don't need to test the "theory" , a break is a jump to the end of the switch statement. Nothing after it gets executed. Since I'm not even sure that the compiler is working properly "testing" anything is not helpful.
If you are able to say that I am mistaken by referring to the spec, that may be useful.
Having read this, I think the best thing to do is remove all use of switch in this application which is a nearly full arduino's worth of code already. [ 16b less coded as if() ]
Perhaps you can explain why the 'L' case gets executed when the switch variable is not equal to 'L'.
ardnut:
Perhaps you can explain why the 'L' case gets executed when the switch variable is not equal to 'L'.
I think it's because without the break, both cases are actually OR'd, and all statements are carried out if either or both cases is true. Without the break, the two cases are evaluated at the same time; since one is true, both prints happen.
What seems to happen is that the second 'case' without a break is a syntax error that fails to get reported and compiler happily carries on to the subsequent code having simply ignored it.
But if you do want to OR two cases, the correct was is to do it with no break, as explained to me a long while ago here:
switch (value) {
case 1041:
case 3089:
// statements
break;
}
Admittedly, your case is subtly different in that you have statements after each case and mine has all the statements after both before the single break.
But I'm inclined to suggest you move past this, bung the break in and have code that works.....