It appears I need to use a break in each case block in my switch statement using C#.
I can see the reason for this in other languages where you can fall through to the next case statement.
Is it possible for case blocks to fall through to other case blocks?
Thanks very much, really appreciated!
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
Yes, you can fall through to the next case block in two ways. You can use empty cases, which don't need a break, or you can use goto
to jump to the next (or any) case:
switch (n) {
case 1:
case 2:
case 3:
Console.WriteLine("1, 2 or 3");
goto case 4;
case 4:
Console.WriteLine(4);
break;
}
The enforcement of "break" is there to stop bugs. If you need to force a fall-thru then use "goto case " (replace the with appropriate value)
the following example shows what you can do:
switch(n)
{
case 1:
case 2:
//do something for 1+2
//...
goto case 3;
case 3:
//do something for 3, and also extra for 1+2
//...
break;
default:
//do something for all other values
//...
break;
}
See http://msdn.microsoft.com/en-us/library/06tc147t%28VS.80%29.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With