C 中 do while 循环条件错误
error on do while loop condition in C
我在尝试这部分程序时在 Xcode 中收到 "code will never be executed" 警告。警告出现在 do while 循环的 while 部分的条件中。如果未输入列表中显示的数字之一,则应该打印以下字符串。有什么建议吗?
#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
OR 条件只需要一个条件就可以计算为真,因此如果用户输入为 1
Input!=1 //evaluates to false
Input!=2 //becomes True
以此类推
使用 && 运算符这将检查如果您的输入不是 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;
}
虽然不是 1 也不是 2 也不是 3 也不是 4 它将再次重复
我在尝试这部分程序时在 Xcode 中收到 "code will never be executed" 警告。警告出现在 do while 循环的 while 部分的条件中。如果未输入列表中显示的数字之一,则应该打印以下字符串。有什么建议吗?
#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
OR 条件只需要一个条件就可以计算为真,因此如果用户输入为 1
Input!=1 //evaluates to false
Input!=2 //becomes True
以此类推
使用 && 运算符这将检查如果您的输入不是 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;
}
虽然不是 1 也不是 2 也不是 3 也不是 4 它将再次重复