The switch statement in a while loop in C

There are several messages about switch statements in while loops, with the exception of the fact that none of them executed in C, at least from what I saw. C ++ can create logical expressions that I know of, but not in C. I have a while loop containing a switch control. However, when I write break instructions on my switch, it goes back to the beginning of the loop and makes my program work forever. Ignore the functions that I use because they work exactly. I just need to clarify how I handled the nest. Thank!

Here is my main.c:

while(1)
{
    printf("0) Exit\n1) List Tasks\n2) Add Task\n");                                            
    printf("3)Delete Task\n4) Add Task From File\n");                                           
    printf("What would you like to do?\n");
    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\n", p);
     }
}
+5
source share
6 answers

break; switch . , goto , return .

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

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

C. int.

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

C , .

#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);
        }
    }
+13

C. C 1999 stdbool.h bool. C, , Visual Studio 2012 (!), , ints:

int keep_looping = 1;
while (keep_looping) {
    ....
    if (....)
        keep_looping = 0;
}
+1

++ , , C.

? , 0 == 0, ? bool, , #include <stdbool.h>.

, break , . , , . . !

. . switch ( t23 > es, }). .

, . . , , , . , :

do {
    /* ... */
} while (p != 0);

, return , , exit(0); . , /.

switch , if/else if/else. if/else if/else, break; . switch, , .

goto . , , .

+1

. .

while(1)

- , , while

0

Your break leaves the switch statement and then continues into

while(1)

forever.

0
source

use exit (0);
where do you want to go out.

In this partcular case you have to put the condition in the while loop parameter

Other methods of exit while loop may be used: return. But it will also exit the current function.

A suitable solution might be to use goto label;inside the switch, and then use label: statement;outside the while loop.

For instance:

while(1){
  switch(x){

   case 0:goto label;

   default:break;
   }
}
label:
printf("out of while loop");
0
source

All Articles