简体   繁体   English

在C中退出while循环而不中断

[英]Exit from while loop without break 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'm trying to exit the while-loop with the flag "stop", but it doesn't work and it simply continues with the rest of the code below. 我正在尝试使用标志“停止”退出while循环,但它不起作用,它只是继续下面的其余代码。 Is there any way to exit this while-loop with a flag and without using break? 有没有办法退出这个带循环的while循环而不使用break?

You can do it in several ways, all of which are grossly inferior to using break : 你可以用几种方式来做,所有这些都远远不如使用break

  • You could add else and increase code nesting of the // more code part, or 你可以添加else并增加// more code部分的代码嵌套,或者
  • You could set the variable and use continue instead of break to confuse your readers, or 您可以设置变量并使用continue而不是break来混淆您的读者,或者
  • You could add a label and use goto to infuriate your co-workers 您可以添加标签并使用goto来激怒您的同事

The best approach is to use break , and drop the stop variable altogether, replacing it with a "forever" loop: 最好的方法是使用break ,并完全删除stop变量,将其替换为“forever”循环:

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

This construct is idiomatic in situations when none of the three built-in loop give you a particularly good fit, ie when the decision to break or continue is made in the middle of the loop, as opposed to the top of the loop (as in for and while loops) or the bottom of the loop (as in do / while loop). 当三个内置循环都没有给你一个特别好的拟合时,即当在循环的中间做出决定中断或继续时,而不是循环的顶部时,这个结构是惯用的(如在forwhile循环)或循环的底部(如do / while循环)。

Note: The reason why your code does not end the loop when you set the variable is that loop condition is not checked continuously. 注意:设置变量时代码不会结束循环的原因是不会连续检查循环条件。 Rather, it is checked once before each iteration begins. 而是在每次迭代开始之前检查一次。 After that the condition may become invalid at any point inside the body of the loop. 之后,条件可能在循环体内的任何点变得无效。

The only way I see is to put in conditional the more code below 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 only way using a 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 a nazi but this should definitely be a do ... while PS :也许我是一个纳粹分子,但这绝对应该是一个do ... while

 int stop = 0;

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

First replace 首先替换

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

with

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

otherwise the if is never true - based on the choices offered above, that's what you want to do. 否则if永远不会是真的 - 基于上面提供的选择,这就是你想要做的。

Then ensure the "more code" is not executed in this case 然后确保在这种情况下不执行“更多代码”

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

in your current code, even if stop is set to 1, you do the "more stuff". 在您当前的代码中,即使将stop设置为1,您也可以执行“更多内容”。

You could also test the opposite, and add continue to the OK code, else set stop 你也可以测试相反的,并添加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;

I hope FRUIT_SHAKE_CHOICE is macro(char value) 我希望FRUIT_SHAKE_CHOICE是宏(char值)

another thing is because of scanf("%c", &typeChoice); 另一件事是因为scanf("%c", &typeChoice); buffer is not getting clear before 2nd input 缓冲区在第二次输入之前没有变得清晰

Either use (give the space) or use getchar() / fgetc() 使用(给空间)或使用getchar() / fgetc()

scanf(" %c", &typeChoice);

Here is my understanding 这是我的理解

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM