C 中 while 循环中的 Switch 语句 [英] Switch statement within while loop in C

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

问题描述

有几篇关于 while 循环中的 switch 语句的帖子,但事实上它们都不是用 C 完成的,至少从我所看到的情况来看是这样.C++ 可以创建布尔表达式,我知道,但不是在 C 中.我有一个包含 switch 控件的 while 循环.但是,当我在 switch 中编写 break 语句时,它会回到循环的开头并使我的程序永远运行.忽略我使用的功能,因为它们肯定有效.我只需要澄清一下我对嵌套的处理.谢谢!

There are several postings concerning switch statements within while loops, except for the fact that none of them are done in C, at least from what I've seen. C++ can create boolean expressions, which I'm aware of, but not in C. I have a while loop that contains a switch control. However, when I write break statements within my switch, it goes back to the beginning of the loop and makes my program run forever. Ignore the functions I use, for they work for sure. I just need some clarification on my handling of the nesting. Thanks!

Here is my main.c:

while(1)
{
    printf("0) Exit
1) List Tasks
2) Add Task
");                                            
    printf("3)Delete Task
4) Add Task From File
");                                           
    printf("What would you like to do?
");
    fgets(buf1, 50, stdin);                                                                     
    p = atoi(buf1);
    switch(p)
    {
          case 0: 
            break;
          case 1: 
            printTaskList(pTaskList);
            break;
          case 2:
            printf("Enter task name: ");
            fgets(buf2,100,stdin);
            printf("Enter task priority: ");
            fgets(buf3,100,stdin);
            printf("Enter task start date: ");
            fgets(buf4,50,stdin);
            pTask = makeTask(buf2,buf4,buf3);
            addTaskToEnd(pTaskList,pTask);
            break;
          case 3:
            printTaskList(pTaskList);
            printf("What task would you like to delete? ");
            fgets(buf6,50,stdin);
            taskNum = atoi(buf6);
            removeTask(pTaskList,taskNum);
            break;
          case 4:
            printf("Enter the filename ");
            fgets(buf7,50,stdin);
            break;
          default:
            printf("ERROR: %d: Incorrect menu option
", p);
     }
}

推荐答案

break; 将退出最近的封闭 switch 或循环.要跳转两个级别,您必须使用可怕的 goto,或者重新组织以便 return 将完成所需的语义.

break; will exit out of the nearest enclosing switch or loop. To jump two levels, you'll have to use the dreaded goto, or reorganize so a return will accomplish the desired semantics.

while(1) {
    switch(x) {
    case 0: goto EndWhile;
    }
}
EndWhile: ;

void loop() {
    while(1) {
        switch(x) {
        case 0: return;
        }
    }
}
//...
loop();
//...

您完全可以在 C 中使用 布尔表达式.只需使用 int.

You can totally use a Boolean expression in C. Just use an int.

int quit = 0;
while(!quit) {
    switch(x) {
    case 0: quit = 1; break;
    }
}

C 也有一个布尔数据类型,如果你真的需要的话.

C also has a boolean data type, if you really want it.

#include <stdbool.h>

bool quit = false;
while(!quit) {
    switch(x) {
    case 0: quit = true; break;
    }
}

还有一个选择,完全疯了.

#include <setjmp.h>

jmp_buf jbuf;
if (!setjmp(jbuf))
    while(1) {
        switch(x) {
        case 0: longjmp(jbuf, 1);
        }
    }

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

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