Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 248549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:21:08+00:00 2026-05-11T21:21:08+00:00

This question in mainly pointed at C/C++, but I guess other languages are relevant

  • 0

This question in mainly pointed at C/C++, but I guess other languages are relevant as well.

I can’t understand why is switch/case still being used instead of if/else if. It seems to me much like using goto’s, and results in the same sort of messy code, while the same results could be acheived with if/else if’s in a much more organized manner.

Still, I see these blocks around quite often. A common place to find them is near a message-loop (WndProc…), whereas these are among the places when they raise the heaviest havoc: variables are shared along the entire block, even when not propriate (and can’t be initialized inside it). Extra attention has to be put on not dropping break’s, and so on…

Personally, I avoid using them, and I wonder wether I’m missing something?

Are they more efficient than if/else’s?
Are they carried on by tradition?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:21:09+00:00Added an answer on May 11, 2026 at 9:21 pm

    Summarising my initial post and comments – there are several advantages of switch statement over if/else statement:

    1. Cleaner code. Code with multiple chained if/else if ... looks messy and is difficult to maintain – switch gives cleaner structure.

    2. Performance. For dense case values compiler generates jump table, for sparse – binary search or series of if/else, so in worst case switch is as fast as if/else, but typically faster. Although some compilers can similarly optimise if/else.

    3. Test order doesn’t matter. To speed up series of if/else tests one needs to put more likely cases first. With switch/case programmer doesn’t need to think about this.

    4. Default can be anywhere. With if/else default case must be at the very end – after last else. In switch – default can be anywhere, wherever programmer finds it more appropriate.

    5. Common code. If you need to execute common code for several cases, you may omit break and the execution will “fall through” – something you cannot achieve with if/else. (There is a good practice to place a special comment /* FALLTHROUGH */ for such cases – lint recognises it and doesn’t complain, without this comment it does complain as it is common error to forgot break).

    Thanks to all commenters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.