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 7775815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:43:15+00:00 2026-06-01T17:43:15+00:00

From what I’ve read from various answers to similar questions, switch cases are compiled

  • 0

From what I’ve read from various answers to similar questions, switch cases are compiled differently under some circumstances.

I have a few scenarios in mind but I’m not sure how they will compile.

Scenario 1

A switch case which takes an enum value. The cases range from 0 – 99 and are ordered.

Scenario 2

A switch case which takes an enum value. The cases range from lets say, 0 – 30, 50 – 80, 100 – 150. Unordered. Will this compile differently from the scenario above?


Basically I’d like to know how the switch cases in the scenarios will be compiled and if any differences exist between the two scenarios. Thanks!

edit: I should have mentioned that one of my biggest concerns is how many checks it would take to match the case. If statements are linear so for scenario one, if it was an if-else-if it would take at most 100 checks, unless I am mistaken. But how is this handled with switch-cases? What kind of optimizations does the compiler make?

  • 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-06-01T17:43:16+00:00Added an answer on June 1, 2026 at 5:43 pm

    There is no way to tell in general. The C++ standard makes hardly any assumption about the code which is generated. This means the compiler is free to almost (yes, there are exceptions) re-order you code, as long as the semantics stays the same. Optimisation was often called “the black magic of compiler construction” when I took courses in that topic.

    Having this in mind, your compiler could spew out the same or different code. It may spit out the same code for some optimisation levels and completely different code for others. It may completely depend on the code you are using in the case blocks themselves. If you have the same code at two points, it might just optimize it to use that code once, or it might not.

    The only way to know what will happen is to look at the output of a compiler with certain fixed flags and see what happens. And once you found out what happens, do not rely on it, it might change in the next version of the compiler.

    If you are programming always keep to the guarantees the language standard gives you and never assume anything beyond that.

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

Sidebar

Related Questions

From my website, I have a button which calls a method and then generates
For some reason, after submitting a string like this Jack’s Spindle from a text
From the paper of bigtable. bigtable I read this: Each METADATA row stores approximately
From what I have seen, if you are building a OSX desktop HTML5 app
From what I've read so far, Windows Phone 7 does not support a Sql
I have a text area in my form which accepts all possible characters from
From my SQLite database i pulled all of the entries that have Liquor in
From the manpage of the dbus-monitor command, I know that I can use some
From what I've read, the iPhone 5 will use a new architecture, armv7s .
From the Writing R Extensions Manual , I read that As from R 2.14.0

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.