“case”内的 If、else-if 和 else 语句可以在“case”内执行。对于 switch-case-break 语句?
我知道编写 case break 语句的语法,但我想知道是否允许这样做:
代码:
case 'p':
{
printf("Give number: ");
scanf("%d, &int_1);
if int_1=5;{
printf("Played: you win");
}
break;
}
基本上我只是想知道这是否可以做到,我知道代码不完整,但我不想任何人都认为我试图得出具体答案。我只是寻求更好地理解将条件应用于我的程序。谢谢。
编辑:除了标签之外,我没有指定,以防万一不清楚,这是在 C 中。
I know the syntax for writing a case break statement, but I'm wondering if this is allowed:
CODE:
case 'p':
{
printf("Give number: ");
scanf("%d, &int_1);
if int_1=5;{
printf("Played: you win");
}
break;
}
Basically I'm just wondering if this is something that's possible to do, I know the code is incomplete but I don't want anyone to think I'm trying to elicit specific answers. I simply seek a better understanding of applying conditionals to my programs. Thank you.
EDIT: Other than in the tags, I didn't specify so just in case this isn't clear, this is in C.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这是允许的。但是您还忘记了
if
周围的(
parentheses)
:应该是:
所以如果它没有编译,这就是原因。
Yes it's allowed. But you also forgot the
(
parentheses)
around yourif
:should be:
So if it wasn't compiling, this is the reason.
简短的答案是肯定的,您可以在
swtich
/case
语句内嵌套if
(反之亦然)。如果您非常想要,您可以有一个包含switch
的循环,其中包含多个if
等。底线:通常会对嵌套各种语句施加限制出于品味和可读性等考虑,而不是语言内置的限制。
The short answer is yes, you can nest an
if
inside ofswtich
/case
statement (or vice versa). If you want to badly enough, you could have a loop containing aswitch
containing severalif
s, etc.Bottom line: the limit on nesting various kinds of statements is normally imposed by such considerations as taste and readability, not limitations built into the language.