替代功能
#include <stdio.h>
void main(void)
{
int price;
scanf("%d", &price);
switch (price)
{
case 1000: // what i want - case pay >= 1000
// code (no break - intentional)
case 500: // ....... - case pay >= 500
// code
default:
break;
}
}
我是新手。在没有中断
的情况下,是否有任何替代方案 ,并且还可以在switch
中使用比较而不是常数?
#include <stdio.h>
void main(void)
{
int price;
scanf("%d", &price);
switch (price)
{
case 1000: // what i want - case pay >= 1000
// code (no break - intentional)
case 500: // ....... - case pay >= 500
// code
default:
break;
}
}
I'm new to this. Is there any alternative to switch
without break
and also able to use comparisons, not constant, in a switch
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需系列
如果
语句,则:#define
对这些魔术数字(1000,500 ...)
。或使用枚举
枚举它们。switch()
看起来像一个函数,但它是一个语句块。Just series of
if
statements then :#define
for those magic numbers(1000, 500 ...)
. Or Enumerate them usingenum
.switch()
looks like a function, it's a statement block.正如其他人所说,如果块比
switch> switch> switch
语句(其中 故意 fall through是在C编程世界中,许多人皱着眉头)。但是,如果要将一个数字分为固定大小的块范围(在您的情况下,大小为500),则可以将数字除以该块,并将结果用作
switch 变量。另外,请注意,
默认
案例不必是 final 案例 - 它甚至可以在任何地方,甚至首先。在下面的示例中,由于秋天的秋季(即
break;
在任何一个case
块中),所以案例0:
代码将用于任何输入;对于少于500的输入(即 Integer disement将导致0),仅该情况将执行。对于500范围内999的数字(将给出1),案例1
和案例0
代码将运行;而且,对于数字&gt; = 1000,默认
案例将运行,然后是其他两个块。 (此代码需要正面Price
!!)正如我所说,我通常不建议在情况下使用
switch
) 像这样;但是,尽管如此,但实际上,添加额外的块/条件(例如1000-1499范围内的价格代码)实际上非常容易代码。此外,当“范围”的数量变得足够大(例如,超过3)时,使用这种switch> switch> switch
而不是链条,则可以说更清晰如果语句/块。注意:为了增加清晰度 - 如果您的编译器支持它(Clang和GCC会这样做,但我不知道MSVC的同等用途) - 那么您可以在任何地方添加
__属性__((fallthrough));
语句依靠任何(隐含的)跌落 - 尽管从一个案例块到另一个案例块。在上面显示的代码中,将在案例1:
和案例0:
标签之前立即添加此类语句。As others have said, this problem is better suited to consecutive
if
blocks than to aswitch
statement (where the use of deliberate fall-through is frowned-upon by many in the C programming world).However, if you want to divide a number into ranges of fixed-size blocks (in your case, that size is 500), then you can divide your number by that block-size and use the result as the
switch
variable. Also, note that thedefault
case doesn't have to be the final case – it can be anywhere, even first.In the below example, because of the fall-through (i.e. no
break;
statements in any of thecase
blocks), thecase 0:
code will be run for any input; for inputs less than 500 (i.e. the integer division will result in 0), only that case will execute. For numbers in the range 500 thru 999 (division will give 1), thecase 1
andcase 0
code will run; and, for numbers >= 1000, thedefault
case will run, followed by the other two blocks. (This code requires a positive value forprice
!)As I have said, I would generally not recommend using a
switch
in cases (poor pun) like this; but, that advice notwithstanding, it is then actually quite easy to add extra blocks/conditions, such as code for prices in the 1000 - 1499 range, where you could just insert acase 2:
block to the code. Furthermore, when the number of 'ranges' becomes sufficiently large (say, more than 3), then it does arguably become clearer to use such aswitch
, rather than a chain ofif
statements/blocks.Note: For added clarity – if your compiler supports it (clang and gcc do but I'm not aware of an equivalent for MSVC) – then you can add
__attribute__((fallthrough));
statements wherever you are relying on any (implied) fall-though from one case block to the next. In the code shown above, such statements would be added immediately before thecase 1:
andcase 0:
labels.使用链式
如果…else
语句:Use chained
if … else
statements: