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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:38:23+00:00 2026-06-02T05:38:23+00:00

Can anyone explain what the trade off (even if it’s negligible) would be between

  • 0

Can anyone explain what the trade off (even if it’s negligible) would be between using if, if else, or switch in a sizable block of code similar to the following? Is the situation different if it were comparing a String or another Object instead of an int? The examples are in Java but it is meant as a general question.

EDIT

As several answers stated, a switch is going to be faster and should probably be used if there are more than a few cases. However, nobody has commented on if vs if else when in a long chain like this. What sparked this question is that I am frequently creating these blocks where a switch can’t be used because most of the cases require multiple expressions. I guess excluding the else feels sloppy, but it isn’t really necessary so why include it?

public String getValueString(int x) {
    if (x == 1) return "one";
    if (x == 2) return "two";
    if (x == 3) return "three";
    if (x == 4) return "four";
    ...
    return null;
}

VS

public String getValueString(int x) {
    if (x == 1) return "one";
    else if (x == 2) return "two";
    else if (x == 3) return "three";
    else if (x == 4) return "four";
    ...
    return null;
}

VS

public String getValueString(int x) {
    switch(x) {
        case 1: return "one";
        case 2: return "two";
        case 3: return "three";
        case 4: return "four";
        ...
    }
    return null;        
}
  • 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-02T05:38:28+00:00Added an answer on June 2, 2026 at 5:38 am

    If you have a lot of cases, then the switch approach is the preferred method. The reason is because the first two requires essentially a linear search through all the if-statements. So it is O(N) to the number of cases you have.

    On the other hand, switch statements are optimized differently and can be either O(log(N)) or even O(1) for finding that correct case.


    How can the compiler achieve O(log(N)) or even O(1)?

    • Binary search of the case values will allow it to be done in O(log(N)).
    • If the case values are dense enough, the compiler may even use a jump table indexed by the case variable. In that case it is O(1).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone explain to me why there is a dramatic difference in performance between
Can anyone explain why this code: {% form_theme form _self %} {% block avo_gallery_upload_widget
Can anyone explain the pros and cons to using Data.Text and Data.ByteString.Char8 data types?
Can anyone explain why DbgView misses some of my trace writes ? I'm using
Can anyone explain me what is the difference between execution plan and explain plan.
Can anyone explain me what is a difference between these lines of code char
I'm using the code below and receiving the FileNotFound exception. Can anyone explain why
Can anyone explain what advantages there are to using a tool like MSBuild (or
Can anyone explain to me why I would want to use IList over List
Can anyone explain to me why this program: for(float i = -1; i <

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.