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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:28:08+00:00 2026-05-10T16:28:08+00:00

Switch statement fallthrough is one of my personal major reasons for loving switch vs.

  • 0

Switch statement fallthrough is one of my personal major reasons for loving switch vs. if/else if constructs. An example is in order here:

static string NumberToWords(int number) {     string[] numbers = new string[]          { '', 'one', 'two', 'three', 'four', 'five',            'six', 'seven', 'eight', 'nine' };     string[] tens = new string[]          { '', '', 'twenty', 'thirty', 'forty', 'fifty',            'sixty', 'seventy', 'eighty', 'ninety' };     string[] teens = new string[]         { 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen',           'sixteen', 'seventeen', 'eighteen', 'nineteen' };      string ans = '';     switch (number.ToString().Length)     {         case 3:             ans += string.Format('{0} hundred and ', numbers[number / 100]);         case 2:             int t = (number / 10) % 10;             if (t == 1)             {                 ans += teens[number % 10];                 break;             }             else if (t > 1)                 ans += string.Format('{0}-', tens[t]);         case 1:             int o = number % 10;             ans += numbers[o];              break;         default:             throw new ArgumentException('number');     }     return ans; } 

The smart people are cringing because the string[]s should be declared outside the function: well, they are, this is just an example.

The compiler fails with the following error:

 Control cannot fall through from one case label ('case 3:') to another Control cannot fall through from one case label ('case 2:') to another 

Why? And is there any way to get this sort of behaviour without having three ifs?

  • 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. 2026-05-10T16:28:09+00:00Added an answer on May 10, 2026 at 4:28 pm

    (Copy/paste of an answer I provided elsewhere)

    Falling through switch–cases can be achieved by having no code in a case (see case 0), or using the special goto case (see case 1) or goto default (see case 2) forms:

    switch (/*...*/) {     case 0: // shares the exact same code as case 1     case 1:         // do something         goto case 2;     case 2:         // do something else         goto default;     default:         // do something entirely different         break; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a switch statement similar to this one: switch (number) { case 1:
I'm getting some very strange behaviour using the following switch statement: string recognise_mti(int mti_code)
Possible Duplicate: Switch statement fallthrough in C#? The following code is illegal in C#
I have the following switch statement, and when the string foo = FOO then
Rather than use a hard-coded switch statement where you pass it the string name
How would I make a switch statement populate a list, or comma delimited string?
A switch statement consists of cases... But is there any else case for all
I'm wondering why fallthrough isn't allowed in a type switch statement in golang. According
I have recently learned that you can use a neat switch statement with fallthrough
I have a switch statement such as the one below: switch (condition) { case

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.