473,472 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

switch

In a switch statement must a supply a default or what is the default by
default?

Is

switch(foo)
{
case 1:
return 1;
default:
break;
}

the same as

switch(foo)
{
case 1:
return 1;
}

Thanx,
Christopher
Jul 19 '05 #1
6 6969
Christopher wrote:
In a switch statement must a supply a default or what is the default
by default?
You supply one if you want to. If you do not supply one then if none of the
labels match the switch will be "ignored", meaning that it means what you
just wrote below:
switch(foo)
{
case 1:
return 1;
default:
break;
}


--
Attila aka WW
Jul 19 '05 #2
Christopher wrote:
In a switch statement must a supply a default or what is the default by
default?

Is

switch(foo)
{
case 1:
return 1;
default:
break;
}

the same as

switch(foo)
{
case 1:
return 1;
}


Yes, it is.

If you don't specify a default and the input doesn't match any of the
cases, then simply nothing in the switch is called.

Jul 19 '05 #3
"Christopher" <cp***@austin.rr.com> wrote in message news:<su******************@twister.austin.rr.com>. ..
In a switch statement must a supply a default or what is the default by
default?

Is

switch(foo)
{
case 1:
return 1;
default:
break;
}

the same as

switch(foo)
{
case 1:
return 1;
}


They are the same.
GJD
Jul 19 '05 #4

Christopher <cp***@austin.rr.com> wrote in message
news:su******************@twister.austin.rr.com...
In a switch statement must a supply a default or what is the default by
default?

Is

switch(foo)
{
case 1:
return 1;
default:
break;
}

the same as

switch(foo)
{
case 1:
return 1;
}

Thanx,
Christopher


The first 'case' value which matches the 'argument'
to 'switch' causes that 'case clause' to be executed,
and continues linearly until a 'break' statement or
the closing brace is encountered. If none of the 'cases'
match and a 'default' case is defined, then that portion
is executed, also until a 'break' or the closing brace
is encountered. If none of the 'cases' match, and no
'default' case is defined, control flow passes to after
the closing brace.

Note that the 'default' case need not be listed last.

Some like to always use a 'default' case whether it
does anything or not, but you only really need it
if you actually want to execute some code in the
'default' case.
-Mike

Jul 19 '05 #5

"Mike Wahler" <mk******@mkwahler.net> wrote in message news:B3****************@newsread4.news.pas.earthli nk.net...

The first 'case' value which matches the 'argument'
to 'switch' causes that 'case clause' to be executed,


Actually, it's the only case that matches. The language
does not allow for duplicate cases.

Jul 19 '05 #6

Ron Natalie <ro*@sensor.com> wrote in message
news:3f*********************@news.newshosting.com. ..

"Mike Wahler" <mk******@mkwahler.net> wrote in message

news:B3****************@newsread4.news.pas.earthli nk.net...

The first 'case' value which matches the 'argument'
to 'switch' causes that 'case clause' to be executed,


Actually, it's the only case that matches. The language
does not allow for duplicate cases.


Of course. I didn't even consider that.
Actually, I didn't know for sure if duplicates
were allowed, since I never had occasion to
try to use them. What would be the point, right? :-)

Thanks.

-Mike

Jul 19 '05 #7

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

Similar topics

14
by: Rudi Hansen | last post by:
I dont seem to be able to find the switch statement in Python. I would like to be able to do switch(var) case 1 : print "var = 1" case 2: print "var = 2"
10
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...
5
by: Bryan Parkoff | last post by:
C++ programmers and I tried to experience by writing 65536 switch cases. It is too large for Windows XP and other operating system to handle. It looks like that JMP Table obtains 256K bytes for...
10
by: clueless_google | last post by:
hello. i've been beating my head against a wall over this for too long. setting the variables 'z' or 'y' to differing numbers, the following 'if/else' code snippet works fine; however, the ...
65
by: He Shiming | last post by:
Hi, I just wrote a function that has over 200 "cases" wrapped in a "switch" statement. I'm wondering if there are performance issues in such implementation. Do I need to optimize it some way? ...
3
by: pgraeve | last post by:
I am a convert from VB to C# so bear with me on this "conversion" question C# switch statement seems to be the closest relative to VB's Select Case. I used VB's Select Case statement liberally. ...
11
by: ME | last post by:
In C# the following code generates a compiler error ("A constant value is expected"): public void Test(string value) { switch (value) { case SimpleEnum.One.ToString(): MessageBox.Show("Test...
12
by: | last post by:
Is it fine to call another method from Switch? Eg. Switch (stringVar) { case ("a"): somVar = "whatever"; Another_Method(); //call another method return;
7
by: Rohit | last post by:
Hi, I am working on a switch module which after reading voltage through a port pin and caterogizing it into three ranges(open,low or high), passes this range to a function switch_status() with...
11
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
Can switch statements be nested? I've got a large routine that runs off of a switch statement. If one of the switches in switch #1 is true, that enters switch statement #2. Some of the...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.