element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Embedded and Microcontrollers
  • Technologies
  • More
Embedded and Microcontrollers
Embedded Forum The Case Statement
  • Blog
  • Forum
  • Documents
  • Quiz
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Embedded and Microcontrollers to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 4 replies
  • Subscribers 481 subscribers
  • Views 485 views
  • Users 0 members are here
Related

The Case Statement

JohnDSiviter
JohnDSiviter over 15 years ago

I have wrote the following:

 

  if (PA!=NAPA)
  {
   switch(NAPA)
   {
      case 0x00:
        switch (PA) //check last state
        {
         case 0x02:myvolume++; break;
         case 0x01:myvolume--; break;
        }
       break;

       case 0x01:
        switch (PA)
        {
         case 0x00:myvolume++; break;
         case 0x03:myvolume--; break;
        }
       break;

      case 0x03:
        switch (PA)
        {
         case 0x01:myvolume++; break;
         case 0x02:myvolume--; break;
        }
       break;
         
       case 0x02:
        switch (PA)
        {
         case 0x03: myvolume++; break;   
         case 0x00: myvolume--; break;
        }
       default:break;
   }

  }

 

My question is, is it possible to combine multiple cases somehow, like:

 

case 0x00 OR case 0x01, if I could do something like this I could reduce it further.

 

This is a routine to detect the direction of a greyscale(or Manchester) encoder and change the volume accordingly.

  • Sign in to reply
  • Cancel
  • firatkocak
    firatkocak over 15 years ago

    hi,

     

    My answer is NO. Case statements should be constant and not conditional. But you can write something a bit more clear,

     

    case 0x00 :

    case 0x01 : do_something(); break;

     

    Firat

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 15 years ago

    Firs tof all, consider the constrcution

    switch (c)

         case a:

              <codeA>

         case b:

              <codeAB>

              break;

         case c:

              ...

     

    In your case you have 2 variables to be checked, you can only do this simultaneously in if... then constructions, wich reads al little bit harder than the switch...case cunstruction

     

    Most important is to maintain readablity in your code. Sometimes it's far more convenient to have code that reads like a magazine, than to have very 'smart' written code that nobody else but you understands.

    Another thing to consider is the fact that the compiler plays a very important role in how efficient the code is. Therefore you could look at the assembly language generated by the compiler. Sometimes the 'smart' solution (with fewer lines in C-code) may produce a lot more machine code than you would expect.

     

    Kris

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 15 years ago

    John, the best way is to do this mathemagically.  If PS is the previous 2 bit state of your gray code input and CS is the current 2 bit state of the gray code input then:

     

         CS = get 2 bit gray code input; eg 00, 01, 11 or 10

         byte DIR = ((CS >> 1) ^ PS) & 1;

         if (DIR == 1)

              do backward stuff

         else

              do forward stuff

         PS = CS;

     

    You might need to draw a bit map to understand the logic.  This will be orders of magnitude more efficient than a collection of case statements.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • JohnDSiviter
    JohnDSiviter over 15 years ago in reply to Former Member

    Hi thanks for the responses, James: thanks you gave me an idea to reduce the boolean equivalent.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube