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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:55:06+00:00 2026-05-26T23:55:06+00:00

I came across a case-switch piece of code today and was a bit surprised

  • 0

I came across a case-switch piece of code today and was a bit surprised to see how it worked. The code was:

switch (blah)
{
case a:
  break;
case b:
  break;
case c:
case d:
case e: 
  {
    /* code here */
  }
  break;
default :
  return;
}

To my surprise in the scenario where the variable was c, the path went inside the “code here” segment. I agree there is no break at the end of the c part of the case switch, but I would have imagined it to go through default instead. When you land at a case blah: line, doesn’t it check if your current value matches the particular case and only then let you in the specific segment? Otherwise what’s the point of having a case?

  • 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-26T23:55:06+00:00Added an answer on May 26, 2026 at 11:55 pm

    This is called case fall-through, and is a desirable behavior. It allows you to share code between cases.

    An example of how to use case fall-through behavior:

    switch(blah)
    {
    case a:
      function1();
    case b:
      function2();
    case c:
      function3();
      break;
    default:
      break;
    }
    

    If you enter the switch when blah == a, then you will execute function1(), function2(), and function3().

    If you don’t want to have this behavior, you can opt out of it by including break statements.

    switch(blah)
    {
    case a:
      function1();
      break;
    case b:
      function2();
      break;
    case c:
      function3();
      break;
    default:
      break;
    }
    

    The way a switch statement works is that it will (more or less) execute a goto to jump to your case label, and keep running from that point. When the execution hits a break, it leaves the switch block.

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

Sidebar

Related Questions

I came across this piece of code, and I can't quite figure out why
I came across this bit of code: n = args[0] as Long [*n..1, n].any{
I am trying Google GIN for our project and I came across a case
Came across something like this today, and was wondering if there was an equivalent
I came across the following expression in someone else's code. I think it's terrible
I came across this question on an interview questions thread. Here is the question:
I came across the following regular expression below on one of my code, anyone
I came accross the following code today and I didn't like it. It's fairly
I came across some java code for doing a prefix search on a JList.
I recently came across the following line of code: var type = (typeof x).toLowerCase();

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.