Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > switch

Reply
Thread Tools

switch

 
 
Christopher
Guest
Posts: n/a
 
      08-20-2003
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


 
Reply With Quote
 
 
 
 
Attila Feher
Guest
Posts: n/a
 
      08-20-2003
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


 
Reply With Quote
 
 
 
 
chris
Guest
Posts: n/a
 
      08-20-2003
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.

 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      08-20-2003
"Christopher" <(E-Mail Removed)> wrote in message news:<suI0b.1002$(E-Mail Removed)>. ..
> 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
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      08-20-2003

Christopher <(E-Mail Removed)> wrote in message
news:suI0b.1002$(E-Mail Removed)...
> 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



 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      08-20-2003

"Mike Wahler" <(E-Mail Removed)> wrote in message news:B3P0b.1090$(E-Mail Removed) 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.



 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      08-20-2003

Ron Natalie <(E-Mail Removed)> wrote in message
news:3f43bf70$0$175$(E-Mail Removed). ..
>
> "Mike Wahler" <(E-Mail Removed)> wrote in message

news:B3P0b.1090$(E-Mail Removed) 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
trunking between Cisco Catalyst 500 switch and other type of switch jshubo@yahoo.com Cisco 3 01-19-2008 12:10 AM
Switch/Switch problem fibre gigbit ethernet hartmut.albers@gmail.com Cisco 2 09-06-2005 04:59 PM
Is a frame switch and an ISDN switch really needed? owmanstubbedmytoe Cisco 2 12-05-2004 07:15 AM
bridge / layer 2 switch / layer 3 switch Joel M. Baldwin Cisco 2 11-06-2003 11:19 PM
difference b/w layer 2 switch and layer 3 switch praveen Cisco 1 10-22-2003 07:19 AM



Advertisments