“case”内的 If、else-if 和 else 语句可以在“case”内执行。对于 switch-case-break 语句?

发布于 2024-12-09 05:41:15 字数 349 浏览 4 评论 0原文

我知道编写 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终弃我 2024-12-16 05:41:15

是的,这是允许的。但是您还忘记了 if 周围的 (parentheses)

if int_1=5;{

应该是:

if (int_1 == 5){

所以如果它没有编译,这就是原因。

Yes it's allowed. But you also forgot the (parentheses) around your if:

if int_1=5;{

should be:

if (int_1 == 5){

So if it wasn't compiling, this is the reason.

帅冕 2024-12-16 05:41:15

简短的答案是肯定的,您可以在 swtich/case 语句内嵌套 if(反之亦然)。如果您非常想要,您可以有一个包含 switch 的循环,其中包含多个 if 等。

底线:通常会对嵌套各种语句施加限制出于品味和可读性等考虑,而不是语言内置的限制。

The short answer is yes, you can nest an if inside of swtich/case statement (or vice versa). If you want to badly enough, you could have a loop containing a switch containing several ifs, 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文