473,569 Members | 2,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

switch, case, and how it sometimes lets you fallthrough

I'm confused as to how fallthrough is limited in switch. For example
the following works:

string switch_test = "a";
switch (switch_test)
{
case "a":
case "b":
case "c":
doSomething(a);
break;
}

but the following does not work:
string switch_test = "a";
switch (switch_test)
{
case "a":
if (<some test>)
break;
case "b":
case "c":
doSomething(a);
}
why is that?
Dec 11 '05 #1
15 3500
A case must either be empty, or exit via a 'jump' statement (break, continue,
return, etc.).

The second example will work if you just include a jump (usually 'break')
after the dosomething(a) line and also ensure that a jump is reached in the
first case (currently, it's only reached if the if test is true).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter

"Benny Raymond" wrote:
I'm confused as to how fallthrough is limited in switch. For example
the following works:

string switch_test = "a";
switch (switch_test)
{
case "a":
case "b":
case "c":
doSomething(a);
break;
}

but the following does not work:
string switch_test = "a";
switch (switch_test)
{
case "a":
if (<some test>)
break;
case "b":
case "c":
doSomething(a);
}
why is that?

Dec 11 '05 #2
This isn't a fix for this, but I wanted to point out that your statement
should also have a 'default' switch to minimize unhandled errors.

like this:

string switch_test = "a";

switch(switch_t est)
{
case "a":
{
if (this)
break;
else
do_somthing_nic e(switch_test);
break;
}
case "b":
case "c:"
{
do_something_na ughty(switch_te st);
break;
}
default:
{
whine_about(swi tch_test);
break;
}
}

"Benny Raymond" wrote:
I'm confused as to how fallthrough is limited in switch. For example
the following works:

string switch_test = "a";
switch (switch_test)
{
case "a":
case "b":
case "c":
doSomething(a);
break;
}

but the following does not work:
string switch_test = "a";
switch (switch_test)
{
case "a":
if (<some test>)
break;
case "b":
case "c":
doSomething(a);
}
why is that?

Dec 12 '05 #3
I know... I just was mainly wondering about how to only break on "a" if
something - otherwise fallthough.

Kevin Kitching wrote:
This isn't a fix for this, but I wanted to point out that your statement
should also have a 'default' switch to minimize unhandled errors.

like this:

string switch_test = "a";

switch(switch_t est)
{
case "a":
{
if (this)
break;
else
do_somthing_nic e(switch_test);
break;
}
case "b":
case "c:"
{
do_something_na ughty(switch_te st);
break;
}
default:
{
whine_about(swi tch_test);
break;
}
}

"Benny Raymond" wrote:

I'm confused as to how fallthrough is limited in switch. For example
the following works:

string switch_test = "a";
switch (switch_test)
{
case "a":
case "b":
case "c":
doSomething(a);
break;
}

but the following does not work:
string switch_test = "a";
switch (switch_test)
{
case "a":
if (<some test>)
break;
case "b":
case "c":
doSomething(a);
}
why is that?

Dec 12 '05 #4
On further review, I think the snip I posted might work. Bear in mind though
that before last week, I haven't written a line of C or C++ in about 10
years, and had not even really looked at C#.

In your doesn't work, the first switch/case, "a", is met - which drops it
down to that block. The first instruction on the block is a logical test -
if it's true, it goes to the 'break;' statement. If it's false, you're
winding up back out of the block, into the switch/case block again, and
falling down the code block.

(I haven't had to talk about programming in really technical terms in a long
time, so my vocabulary is a little rusty. I think you see what I'm getting
at, tho.)

"Benny Raymond" wrote:
I know... I just was mainly wondering about how to only break on "a" if
something - otherwise fallthough.

Kevin Kitching wrote:
This isn't a fix for this, but I wanted to point out that your statement
should also have a 'default' switch to minimize unhandled errors.

like this:

string switch_test = "a";

switch(switch_t est)
{
case "a":
{
if (this)
break;
else
do_somthing_nic e(switch_test);
break;
}
case "b":
case "c:"
{
do_something_na ughty(switch_te st);
break;
}
default:
{
whine_about(swi tch_test);
break;
}
}

"Benny Raymond" wrote:

I'm confused as to how fallthrough is limited in switch. For example
the following works:

string switch_test = "a";
switch (switch_test)
{
case "a":
case "b":
case "c":
doSomething(a);
break;
}

but the following does not work:
string switch_test = "a";
switch (switch_test)
{
case "a":
if (<some test>)
break;
case "b":
case "c":
doSomething(a);
}
why is that?

Dec 12 '05 #5
That's what I would assume... when I hit "a" it goes into the block - if
b, c, or d were there it would go into what's under d because neither b
or c had break statements... However, when I add my "some test" in
there, I get a compile error saying that I cannot fall through - really
quite bizarre since it lets me fall through when I dont have anything
under the case (such as in b and c, or in a, b and c in the first statement)

Kevin Kitching wrote:
On further review, I think the snip I posted might work. Bear in mind though
that before last week, I haven't written a line of C or C++ in about 10
years, and had not even really looked at C#.

In your doesn't work, the first switch/case, "a", is met - which drops it
down to that block. The first instruction on the block is a logical test -
if it's true, it goes to the 'break;' statement. If it's false, you're
winding up back out of the block, into the switch/case block again, and
falling down the code block.

(I haven't had to talk about programming in really technical terms in a long
time, so my vocabulary is a little rusty. I think you see what I'm getting
at, tho.)

"Benny Raymond" wrote:

Dec 12 '05 #6
Benny Raymond wrote:
I'm confused as to how fallthrough is limited in switch. For example
the following works:

string switch_test = "a";
switch (switch_test)
{
case "a":
case "b":
case "c":
doSomething(a);
break;
}

but the following does not work:
string switch_test = "a";
switch (switch_test)
{
case "a":
if (<some test>)
break;
case "b":
case "c":
doSomething(a);
}
why is that?


Hi, there are two errors with the code, one being your problem, and the
second being that you're missing a 'break;' after the second set of case
blocks. To workaround your problem, include a 'goto case "b";' statement,
after your if statement:

string switch_test = "a";

switch (switch_test)
{
case "a":
if ( <some-test> )
break;
goto case "b";
case "b":
case "c":
doSomething(a);
break;
}

-- Tom
OrElse what...
Dec 12 '05 #7
Fallthrough is diallowed in C#. The design team considered fallthrough
one of the aspects of C / C++ that tended to confuse non-C readers, so
they insisted on a "break" in every case to make the semantics clearer.

Then, the only decision was how to designate cases that should be
executed for multiple values. Should they stay with the C syntax:

case "a";
case "b":
case "c":
doAbcCase();
break;

or go with the Pascal synax:

case "a", "b", "c":
doAbcCase();
break;

They decided on the C syntax.

So, it's not really "fallthroug h": it is, rather, a way to indicate
cases that should be executed for more than one value.

As someone who programmed in C for more than 10 years, I can tell you
that the kind of "fallthroug h" you're talking about is something that
good C programmers avoided whenever possible. Psychologically it was to
easy to miss the subtle distinction of the missing "break", and so it
was correspondingly more difficult to maintain the code. I never found
an occasion in ten years in which I had to resort to it, so I don't
miss it in C#.

Dec 12 '05 #8
Benny Raymond <be***@pocketro cks.com> wrote:
I'm confused as to how fallthrough is limited in switch. For example
the following works:

string switch_test = "a";
switch (switch_test)
{
case "a":
case "b":
case "c":
doSomething(a);
break;
}

but the following does not work:
string switch_test = "a";
switch (switch_test)
{
case "a":
if (<some test>)
break;
case "b":
case "c":
doSomething(a);
}
why is that?


You are not allowed to fall through in a C# switch statement. From the C#
specification:

| The "no fall through" rule prevents a common class of bugs that occur in
| C and C++ when break statements are accidentally omitted. In addition,
| because of this rule, the switch sections of a switch statement can be
| arbitrarily rearranged without affecting the behavior of the statement.

In your last example, you are *not* falling through on case "b"---you are
merely using two labels (case "b" and case "c") in a single section.
Dec 12 '05 #9
For the benefit of those looking over our shoulders, so to speak, I offer the
following code - which I wrote and tested quickly in VS2005. It's not very
pretty, and is probably twice as long as it needs to be - C was never my
strong point - and as I pointed out earlier, I'm just now relearing the
language.

Size and efficiency aside, it does the job.

Hope this helps. It's been good practice.

using System;
using System.Collecti ons.Generic;
using System.Text;

namespace casestatementte sting
{
class Program
{
static void Main(string[] args)
{
string thischar = "a";
string nline;
switch(thischar )
{
case "a":
if (sometest(thisc har))
{
Console.WriteLi ne("in switchblock 'a'");
do_something_ni ce("a");
break;
}
else
{
Console.WriteLi ne("in switchblock 'a'");
do_something_na ughty("a");
break;
}
break;
case "b":
case "c":
Console.WriteLi ne("Now in Block C");
do_something_ni ce("a");
do_something_na ughty("a");
break;
default:
Console.WriteLi ne("Now in Default Block");
break;
}
Console.WriteLi ne("Press Enter to Exit");
nline = Console.ReadLin e();
}

static bool do_something_ni ce(string mystring)
{
Console.WriteLi ne("You're a handsome devil!");
return true;
}

static bool do_something_na ughty(string mystring)
{
Console.WriteLi ne("Your father was a hampster and your mother
smells of Elderberries!") ;
return true;
}

static bool sometest(string mychar)
{
if (mychar != "a")
return false;
else
return true;
}
}
}

"Benny Raymond" wrote:
That's what I would assume... when I hit "a" it goes into the block - if
b, c, or d were there it would go into what's under d because neither b
or c had break statements... However, when I add my "some test" in
there, I get a compile error saying that I cannot fall through - really
quite bizarre since it lets me fall through when I dont have anything
under the case (such as in b and c, or in a, b and c in the first statement)

Kevin Kitching wrote:
On further review, I think the snip I posted might work. Bear in mind though
that before last week, I haven't written a line of C or C++ in about 10
years, and had not even really looked at C#.

In your doesn't work, the first switch/case, "a", is met - which drops it
down to that block. The first instruction on the block is a logical test -
if it's true, it goes to the 'break;' statement. If it's false, you're
winding up back out of the block, into the switch/case block again, and
falling down the code block.

(I haven't had to talk about programming in really technical terms in a long
time, so my vocabulary is a little rusty. I think you see what I'm getting
at, tho.)

"Benny Raymond" wrote:

Dec 12 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
10893
by: Myster Ious | last post by:
Polymorphism replaces switch statements, making the code more compact/readable/maintainable/OO whatever, fine! What I understand, that needs to be done at the programming level, is this: a switch-case has a variable (most probably an enumeration) & associated symbols or integral value. Selection is made, base on what symbol/value the...
12
3210
by: junky_fellow | last post by:
Which is better using a switch statement or the if-then equivalent of switch ?
6
449
by: Mark Broadbent | last post by:
How on earth did Anders & co arrive at this awful switch/ case/ select statement? Not only does it lose the abilty to do an intelligent switch a-la VB eg. using >, <, list of values etc .... but it also lacks a "C# feel" e.g. I would have expected a brace pair to be required between each case label .... and the break to prevent fall thru is...
4
29229
by: genc ymeri | last post by:
Hi, I'm trying to do a very easy thing in SWITCH statment in C# but for some reasons I can't do it (I'm getting errors messages,probably I'm missing something) What I want to do is this : int n = aObject.FunctionOfIntergerType; switch (n)
24
4363
by: Mark | last post by:
Hi - how can I get a switch statement to look for a range of values? I have: function payCalc(field, field2) switch(field.value) { case 0-50: field2.value="lower band"; case 51-99: field2.value="mid band"; case 100-9999: field2.value="high band";
11
2510
by: Smithers | last post by:
Just wondering if the sequence of the case(s) in the switch block might impact performance. Suppose I switch on an int, and I have 4 expected cases. Lets say I expect case 3 to happen 95% of the time. Would the switch block be expected to execute faster (meaning "start executing the code for case 3 sooner") when case 3 appears first, as...
5
2881
by: DaemonCoder | last post by:
I have a switch/case statement using numerics for a menu, but when i enter an alphanumeric statment it goes into a loop printing my menu and the default statement from switch/case the default statement. The only way to end it is ctrl-c. the code is below. Can someone please explain this quirk to me? #include<stdio.h> #include<stdlib.h> int...
0
7698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7970
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.