By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

switch

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a

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

P: n/a

"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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.