-7

I just asked a same question, but the question is closed, maybe I broke the rules...or mess.

anyway i know about loop exit like break, while(bool), return, n=1, n=0 while(n), I want another method other than the above one.

i solved this problem with while(mode!=2) thanks for all, to answer my immature question :) i promise hard study by foundation your feedback!

question is closed.

here's my code structure :) ↓

        
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>

int main() {
    
    int  mode=1;
    while (mode != 2)
    {
        printf("\ninput: "); scanf("%d", &mode);
        switch (mode) 
        {
        case 1: printf("\none"); Sleep(1500); break;
        case 2:
            printf("\nsomething");
            Sleep(1500);
            break;

        default: printf("\nStackOverFlow");  break;
        }
    }
    return 0;
}
REDALOE
  • 1
  • 2
  • 3
    Using a boolean state variable instead of a hard-coded infinite loop, is a common way to solve this problem. If it's used as part of the loop condition, or as a separate condition for a `break`, doesn't really matter and depends more on your use-case. – Some programmer dude Feb 21 '23 at 07:00
  • 1
    The problem you're running into is that a `break` at that point refers to the enclosing `switch` statement, not to the `while` loop that contains it. C has no multi-level `break`. – Keith Thompson Feb 21 '23 at 07:05
  • What problem are you trying to solve? What do you need that `break`, `return` and loop conditions lack? – Caleb Feb 21 '23 at 07:14
  • 1
    Does this answer your question? [How to break out of nested loops?](https://stackoverflow.com/questions/9695902/how-to-break-out-of-nested-loops) – nielsen Feb 21 '23 at 07:26
  • Editing the code in the question after it has received answers/comments invalidates them. And your newfound solution can not work. – Harith Feb 21 '23 at 09:43
  • avoid using variable names that are C keywords. – stark Feb 21 '23 at 12:49
  • sorry, My intention was not a duplicate question. – REDALOE Feb 21 '23 at 14:34
  • I edit my code to work now, and as you know scanf is work when SDL check, off condtion for safety. – REDALOE Feb 21 '23 at 14:46
  • I just noticed that I confused the case with the mode variable that I originally had to enter. – REDALOE Feb 21 '23 at 14:56

1 Answers1

0

Use goto:

This will only exit the while loop.

while (true) { 
    ...
    if (some condition) {
        goto CLEANUP;
    }
    ...
}
CLEANUP:
...

exit() the program:

while (true) {
    ...
    if (some condition) {
       exit (EXIT_FAILURE);
       
       /* OR, if in the `main()` function: */
       return EXIT_FAILURE;
    }
    ....
}

abort() the program:

while (true) { 
    ...
    if (some condition) {
        abort ();
    }
    ...
}

Some other alternatives, that will do much more than just exit the while loop, include:

  • The raise() function.
  • The quick_exit() function.
  • The _Exit() function.
  • The _exit function. (Note that this is not defined in the C standard, it is a POSIX extension)
Harith
  • 4,663
  • 1
  • 5
  • 20