-
December 11th, 2019, 01:20 PM
#1
switch{case}
Hello,
In the code below will case 0 to 8 find the conditional statement at case '9'?
Thank you
Code:
switch (key)
{
case NO_KEY: break;
case '.': if(!decOffset)
{
decOffset = true;
}
lcd.print(key);
break;
case 'D': num=0.00;
lcd.setCursor(15,1);lcd.print(" ");
lcd.setCursor(15,1);
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': if(!decOffset)
{
num = num * 10 + (key - '0');
lcd.print(key);
}
else if((decOffset) && (counter <= 1))
{
num = num * 10 + (key - '0');
lcd.print(key);
counter++;
}
break;
}
-
December 11th, 2019, 05:03 PM
#2
Re: switch{case}
If you mean '0' to '9' then yes due to fallthrough, but for this kind of question it is usually faster to write a test program and see for yourself.
-
December 11th, 2019, 07:36 PM
#3
Re: switch{case}
laserlight,
Thank you for your reply. I assumed that the conditional statement at case '9' would be processed. I just took an interest in programming in C++ a couple of weeks ago so I have no idea how to time code.
Ron
-
December 11th, 2019, 07:43 PM
#4
Re: switch{case}
Originally Posted by OldRon
laserlight,
Thank you for your reply. I assumed that the conditional statement at case '9' would be processed. I just took an interest in programming in C++ a couple of weeks ago so I have no idea how to time code.
Ron
Well, dive right in and get coding.
-
December 12th, 2019, 04:48 AM
#5
Re: switch{case}
case statements 'fall through' to the next until either a break statement or the end of the switch is reached. On some compilers, this 'fall through' from one case to the next could generate a warning message as this type of usage is considered exceptional as the statements for each case usually end with a break statement. C++17 conforming compilers allow the [[fallthrough]] attribute to suppress this warning where this case fall through is required (as here) rather than accidental. See https://en.cppreference.com/w/cpp/la...es/fallthrough
Also note that there is the default: which statements are executed if non of the case statements are selected. A case can fall through to the default if present. See https://en.cppreference.com/w/cpp/language/switch
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 12th, 2019, 07:59 AM
#6
Re: switch{case}
Thanks for all of the responses to my question and the manner in which help was given. I decided to start a project based on an Arduino and my first question in the Arduino forum garnered arrogant and combative responses. I'm not a stupid person, I just play one outside of my field of expertise.
Merry Christmas and Happy New Year to All,
Ron
-
December 12th, 2019, 09:25 AM
#7
Re: switch{case}
my first question in the Arduino forum garnered arrogant and combative responses
I concur with your experience. I had similar and I'm an experienced c/c++ programmer!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 12th, 2019, 07:43 PM
#8
Re: switch{case}
I simply asked about the clock speed is and they jumped on me for not telling them what frequency my project required. I have programmed in vb6 for over twenty years and my first program was the full feature Windows based OS for my CNC milling machine. The program was a CNC front end for Galil Motion Control DMC1840 DSP board. The DMC board had it's own foreign language so if I can learn two foreign languages then I can learn 3. The Arduino has my interest so I have motivation to learn C++. It's like the chinese version of vb6.
-
December 13th, 2019, 02:29 PM
#9
Re: switch{case}
Originally Posted by OldRon
I just took an interest in programming in C++ a couple of weeks ago so I have no idea how to time code.
Ah, pardon the lack of clarity: I meant "faster" as in "faster feedback". That is, if you have a question like "will this code below produce this result?", you'll get faster feedback on your question by writing a tiny program to test the code. If the result is not what you expected, then you immediately know the answer is "no" without having to wait for someone to reply. Unfortunately, if the result is what you expected, you might still want to come over here and ask someone to check as you might be running into undefined behaviour or at least implementation defined behaviour, but at least you'll quickly have a tentative answer.
-
December 13th, 2019, 04:28 PM
#10
Re: switch{case}
I'm in the pictorial phase of learning.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|