为什么这个while语句的"或"部分不起作用,但是break语句呢?(对于C)

-1 c while-loop

学习编码,并尝试一个简单的while循环数字猜谜游戏.这个想法是,如果错误猜测的数量达到5,就会导致循环变为"假"并结束.相反,它将循环,直到它被测试站点作为无限循环结束."破解"版本的工作正常.我的问题是为什么if"break"工作,但是|| 设置值= false不起作用?

没有工作的代码片段

while (secret != guess || wrong < 5)
{
    if (guess < secret)
    {
        printf("You guessed too low\n");
        wrong++;
    }
    else 
    {
        printf("you guessed too high\n");
        wrong++;
    }
    printf("Input another guess\n");
    scanf ("%d", &guess);

}
Run Code Online (Sandbox Code Playgroud)

工作代码片段

while (secret != guess)
{
    if (guess < secret)
    {
        printf("You guessed too low\n");
    }
    else 
    {
        printf("you guessed too high\n");
    }
    printf("Input another guess\n");
    scanf("%d", &guess);
    wrong = wrong + 1;
    if (wrong >= 5)
    {
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

bar*_*nos 5

while两种方式表达可以"翻译":

  1. 只要条件成立
  2. 直到情况变得虚假

你似乎对一个稍微复杂的条件存在逻辑混乱.


改变这个:

while (secret != guess || wrong < 5)
// until both of the conditions become false
// as long as either one of the conditions is true
Run Code Online (Sandbox Code Playgroud)

对此:

while (secret != guess && wrong < 5)
// as long as both of the conditions are true
// until either one of the conditions becomes false
Run Code Online (Sandbox Code Playgroud)