Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch statement fallthrough in C#?

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?

like image 752
Matthew Scharley Avatar asked Oct 06 '08 13:10

Matthew Scharley


People also ask

What is Fallthrough in switch case?

Code Inspection: Fallthrough in 'switch' statementReports a switch statement where control can proceed from a branch to the next one. Such "fall-through" often indicates an error, for example, a missing break or return .

What is Fallthrough statement?

Fallthrough in C++ Fall through is a type of error that occurs in various programming languages like C, C++, Java, Dart …etc. It occurs in switch-case statements where when we forget to add a break statement and in that case flow of control jumps to the next line.

What is a switch statement in C programming?

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.

What is the syntax of switch statement?

The syntax of the switch statement is:statement(s); break; case constant 2: statement(s);


1 Answers

(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; } 
like image 117
Alex Lyman Avatar answered Oct 12 '22 22:10

Alex Lyman