CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: switch{case}

  1. #1
    Join Date
    Dec 2019
    Posts
    23

    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;
           }

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    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.

  3. #3
    Join Date
    Dec 2019
    Posts
    23

    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

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,357

    Re: switch{case}

    Quote Originally Posted by OldRon View Post
    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.

  5. #5
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,255

    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++17 Compiler: Microsoft VS2019 (16.7.3)

  6. #6
    Join Date
    Dec 2019
    Posts
    23

    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

  7. #7
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,255

    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++17 Compiler: Microsoft VS2019 (16.7.3)

  8. #8
    Join Date
    Dec 2019
    Posts
    23

    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.

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: switch{case}

    Quote 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.

  10. #10
    Join Date
    Dec 2019
    Posts
    23

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)