简体   繁体   English

C中的while循环条件中的错误

[英]error on do while loop condition in C

I'm getting a "code will never be executed" warning in Xcode when trying out this part of the program. 尝试使用程序的这一部分时,我在Xcode中收到“永远不会执行代码”警告。 The warning appears in the condition for the while part of the do while loop. 该警告出现在do while循环的while部分的条件中。 It is supposed to print the string below if one of the numbers shown in the list is not entered. 如果未输入列表中显示的数字之一,则应在下面打印字符串。 Any suggestions? 有什么建议么?

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


int main()
{

int input;

    do
    {
        printf("Welcome to the Arithmetic Quiz Program!\n\n
               Please choose one of the following by entering the correspoding number:\n\n
               1. Give me an addition problem.\n
               2. Give me a subtraction problem.\n
               3. Give me a multiplication problem.\n
               4. Quit.\n\n");

        scanf(" %i", &input);

    } while ( input!=1 || input!=2 || input!=3 || input!=4 );

return 0;

}

If your do while loop condition has all the conditions possible then it would never evaluate to false to break the loop 如果您的do while循环条件具有所有可能的条件,那么它将永远不会评估为false来破坏循环

OR conditions need only one condition to evaluate to True so if user input is 1 OR条件只需要一个条件就可以评估为True,因此如果用户输入为1

Input!=1 //evaluates to false
Input!=2 //becomes True

And so on 等等

Use && operator this would check that if your input is not 1,2,3,4 it would break 使用&&运算符,这将检查如果您输入的不是1,2,3,4,则会中断

int main()
{

int input;

    do
    {
        printf("Welcome to the Arithmetic Quiz Program!\n\n
               Please choose one of the following by entering the correspoding number:\n\n
               1. Give me an addition problem.\n
               2. Give me a subtraction problem.\n
               3. Give me a multiplication problem.\n
               4. Quit.\n\n");

        scanf(" %i", &input);

    } while ( input!=1 && input!=2 && input!=3 && input!=4 );

return 0;

}

While are not 1 nor 2 nor 3 nor 4 it will repeat again 不是1或2也不是3或4时,它将再次重复

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

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