Exiting a while loop without interruption in C

I have the following code:

void takeOrder(void)
{
int stop = 0;
while(stop != 1)
{
    printf("What is your order?\n");
    printf("%c - fruitShake\n%c - milkShake\n", FRUIT_SHAKE_CHOICE, MILK_SHAKE_CHOICE); 
    scanf("%c", &typeChoice); 
    if(typeChoice != FRUIT_SHAKE_CHOICE || typeChoice != MILK_SHAKE_CHOICE)
    {
        printf("***Error! Wrong type***");
        stop = 1;
    }
    //more code below
}

}

I am trying to exit a while loop with the “stop” flag, but it does not work, and it just continues with the rest of the code below. Is there any way to get out of this while loop with a flag and without using break?

+4
source share
4 answers

The only way I see is to set the conditional part more code below

 int stop = 0;

 while (stop != 1) {
   if (typeChoice == FRUIT_SHAKE_CHOICE || typeChoice == MILK_SHAKE_CHOICE) {
     stop = 1;
   } else {
     // more code below
   }
 }

The second way to use the function:

while (doStuff() == 0);

int doStuff(void) {
   if (typeChoice == FRUIT_SHAKE_CHOICE || typeChoice == MILK_SHAKE_CHOICE) {
     return 1;
   } 

   // more code below

   return 0;
}

PS : Maybe I'm fascist, but it definitely should bedo ... while

 int stop = 0;

 do {
   if (typeChoice == FRUIT_SHAKE_CHOICE || typeChoice == MILK_SHAKE_CHOICE) {
     stop = 1;
   } else {
     // more code below
   }
 } while (stop != 1);
+5
source

You can do this in several ways, all of which are very inferior to use break:

  • You can add elseand increase part code nesting // more codeor
  • continue break, ,
  • goto, .

- break stop, "forever":

for (;;) {
    ...
    if (condition) {
        break;
    }
    ...
}

, , .. , ( for while), ( do/while).

. , , , . , . .

+7

if(typeChoice != FRUIT_SHAKE_CHOICE || typeChoice != MILK_SHAKE_CHOICE)

if(typeChoice != FRUIT_SHAKE_CHOICE && typeChoice != MILK_SHAKE_CHOICE)
                                   ^^^^

if - , .

, " "

if(typeChoice != FRUIT_SHAKE_CHOICE && typeChoice != MILK_SHAKE_CHOICE) {
   printf("Wrong type\n");
   stop = 1;
}
else {
    // more stuff
}

, stop 1, " ".

continue OK, stop

if(typeChoice == FRUIT_SHAKE_CHOICE || typeChoice == MILK_SHAKE_CHOICE) {
    // do more stuff
    continue; // keep going
}
printf("Wrong choice, same player shoot again\n");
stop = 1;
+2

, FRUIT_SHAKE_CHOICE - (char )

- scanf("%c", &typeChoice);

( ), getchar()/fgetc()

scanf(" %c", &typeChoice);

#define FRUIT_SHAKE_CHOICE 'a'
#define MILK_SHAKE_CHOICE 'b'
void takeOrder(void)
{
char typeChoice;
int stop = 0;
while(stop != 1)
{
    printf("What is your order?\n");
    printf("%c - fruitShake\n%c - milkShake\n", FRUIT_SHAKE_CHOICE, MILK_SHAKE_CHOICE); 
    scanf(" %c", &typeChoice); 
    if(typeChoice != FRUIT_SHAKE_CHOICE && typeChoice != MILK_SHAKE_CHOICE)
    {
        printf("***Error! Wrong type***");
        stop = 1;
        continue;// bcz once stop becomes 1 , you don't to want to execute below part, it will jump to while condition checking part & come out from loop
    }
    //more code below
}
}
int main()
{
     takeOrder();
     return 0;
}
0

Source: https://habr.com/ru/post/1690136/


All Articles