开关语句??? [英] switch-statement ???

查看:88
本文介绍了开关语句???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




你可以在开关中指定一个范围 - 声明吗?


开关(i)

{

案例100 - 999:

//做点什么

休息;

案例1000 - 9999 :

//做其他事情

休息;

}


不允许使用以前的语法,我知道,

你可以用''switch'来解决这个问题吗?无需为每个可能的值提供一个案例

声明而不使用''if''?


谢谢

Christian

解决方案

你好Chris,


我们正在讨论这个问题几个月前,答案是

(在C#中),没有。另一方面,如果您在发布的

示例中只有两个范围,则if ... else结构几乎同样有效。如果

你的每个案例陈述只有几个选项,那么案例可以通过这样的方式来兑现




switch( i)

{

案例100:

案例101:

//做点什么

休息;

案例1000:

案例1001:

案例1002:

//做点什么否则

休息;

}


谢谢,

Michael C.,MCDBA

" Chris" < CH ******** @ pandora.be>在留言中写道

news:o2 *********************** @ phobos.telenet-ops.be ...



你可以在开关中指定一个范围 - 声明吗?

开关(i)
{/>案例100 - 999:
//做点什么
休息;
案例1000 - 9999:
//做别的事情
休息;
}

以前的语法是不允许的,我知道,
但是你可以用''switch'来解决这个问题吗?无需为每个可能的值提供案例
声明而不使用''if''?

感谢
Christian



"克里斯" < CH ******** @ pandora.be>写道:

switch(i)
{/>案例100 - 999:




实际编译,但它不会做你想要的。 (减去

符号是减法,所以它相当于case -899。)


你不能指定范围一个C#开关语句。


P.


我认为C#中不支持switch语句中的漏洞>
(设计选择)。


至少,这就是微软内部C#第224页提到的内容

by Tom Archer


周六,2004年10月9日17:32:29 GMT,Michael C < mi ****** @ nospam.org>

写道:

嗨克里斯,

我们在讨论几个月前的那个问题,答案是答案(在C#中),没有。另一方面,如果您在发布的
示例中只有两个范围,那么if ... else结构几乎同样有效。如果
每个案例陈述只有几个选项,案例可以这样下去:

switch(i)
{
案例100:
案例101:
//做点什么
休息;
案例1000:
案例1001:
案例1002:
//做其他事情
休息;
}
谢谢,
迈克尔C.,MCDBA

" Chris" < CH ******** @ pandora.be>在消息中写道
新闻:o2 *********************** @ phobos.telenet-ops.be ...



你能在开关中指定一个范围 - 声明吗?

开关(i)
{
案例100 - 999:
//做点什么
休息;
案例1000 - 9999:
//做别的事情
休息;
}

以前的语法是不允许的,我知道,
但是你可以用''switch'来解决这个问题吗?无需为每个可能的值提供案例
声明而不使用''if''?

感谢
Christian




Hi,

can you specify a range in a switch - statement ?

switch (i)
{
case 100 - 999 :
// do something
break;
case 1000 - 9999:
// do something else
break;
}

Previous syntax is not allowed, I know,
but can you solve this using ''switch'' ? without having to provide a case
statement for every possible value and without using ''if'' ?

thanks
Christian

解决方案

Hi Chris,

We were discussing the heck out of that a few months ago, and the answer is
(in C#), no. On the other hand, if you only have two ranges as in the
example you posted, an if...else construct would be almost as efficient. If
you have only a couple of options per case statement, case can drop-through
like this:

switch (i)
{
case 100:
case 101:
// do something
break;
case 1000:
case 1001:
case 1002:
// do something else
break;
}

Thanks,
Michael C., MCDBA

"Chris" <ch********@pandora.be> wrote in message
news:o2***********************@phobos.telenet-ops.be...

Hi,

can you specify a range in a switch - statement ?

switch (i)
{
case 100 - 999 :
// do something
break;
case 1000 - 9999:
// do something else
break;
}

Previous syntax is not allowed, I know,
but can you solve this using ''switch'' ? without having to provide a case
statement for every possible value and without using ''if'' ?

thanks
Christian



"Chris" <ch********@pandora.be> wrote:

switch (i)
{
case 100 - 999 :



That will actually compile, but it won''t do what you want. (The minus
sign is subtraction, so it''s equivalent to "case -899".)

You can''t specify ranges in a C# switch statement.

P.


I thought fall-throughs in switch statements were not supported in C#
(design choice).

At least, that''s what mentioned on page 224 of Microsoft''s "Inside C#"
by Tom Archer

On Sat, 09 Oct 2004 17:32:29 GMT, "Michael C" <mi******@nospam.org>
wrote:

Hi Chris,

We were discussing the heck out of that a few months ago, and the answer is
(in C#), no. On the other hand, if you only have two ranges as in the
example you posted, an if...else construct would be almost as efficient. If
you have only a couple of options per case statement, case can drop-through
like this:

switch (i)
{
case 100:
case 101:
// do something
break;
case 1000:
case 1001:
case 1002:
// do something else
break;
}

Thanks,
Michael C., MCDBA

"Chris" <ch********@pandora.be> wrote in message
news:o2***********************@phobos.telenet-ops.be...

Hi,

can you specify a range in a switch - statement ?

switch (i)
{
case 100 - 999 :
// do something
break;
case 1000 - 9999:
// do something else
break;
}

Previous syntax is not allowed, I know,
but can you solve this using ''switch'' ? without having to provide a case
statement for every possible value and without using ''if'' ?

thanks
Christian




这篇关于开关语句???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆