简体   繁体   English

关于 C 语言中的 if else 语句

[英]Regarding if else statements in C language

Please explain to me why this code is wrong for the task and below I have explained all the four conditions -[][1]请向我解释为什么此代码对于该任务是错误的,下面我已经解释了所有四个条件 -[][1]

#include stdio.h
int main()    
{
      int n;
      scanf ("%d", &n);     //taking input
      if (n / 2 != 0)
        {
          printf ("Weird");     //checking first condition
        }
      else if (n % 2 == 0 && 2 <= n <= 5)
        {               //checking second condition
          printf ("Not Weird");
        }
      else if (n % 2 == 0 && 6 <= n <= 20)
        {               //checking third condition
          printf ("Weird");
        }
      else if (n % 2 == 0 && n > 20)
        {               //checking fourth condition
          printf ("Not Weird");
        }
      else
        {
          printf ("Error");
        }
      return 0;
    }

this is the image for the question[1]: https://i.stack.imgur.com/OtY7o.png **这是问题的图像[1]: https://i.stack.imgur.com/OtY7o.png **

Testing for Odd or Even测试奇数或偶数

n / 2 != 0 does not test whether n is odd. n / 2 != 0不测试n是否为奇数。 n/2 calculates the quotient that results from dividing n by 2 (rounding any fraction down). n/2计算将n除以 2 所得的商(将任何分数向下舍入)。 So 0/2 is 0, 1/2 is 0, 2/2 is 1, 3/2 is 1, 4/2 is 2, and so on.所以0/2是 0,1 1/2是 0,2 2/2是 1,3 3/24/2 2 是 2,依此类推。 So n / 2 != 0 is true for all n other than −1, 0, and 1.所以n / 2 != 0对于除 -1、0 和 1 之外的所有n都是正确的。

To test whether a number is odd, you can use n % 2 != 0 .要测试一个数字是否为奇数,您可以使用n % 2 != 0 n%2 calculates the remainder from the division. n%2计算除法的余数。 If it is zero, n is even.如果为零,则n是偶数。 If n is not zero, n is odd.如果n不为零,则n为奇数。

Using Else Efficiently有效地使用 Else

Once you have tested whether n is odd using n % 2 != 0 , you do not have to test whether it is even in the else clauses.一旦您使用n % 2 != 0测试了n是否为奇数,您就不必在else子句中测试它是否是偶数。 The else expressions and their statements will be evaluated only if the if expression is false, which happens (after the correction above) only when n is even.仅当if表达式为假时才会评估else表达式及其语句,这仅在n为偶数时发生(在上面的更正之后)。 So we do not need to test again.所以我们不需要再次测试。

Testing For an Interval测试间隔

In C, 2 <= n <= 5 does not test whether n is between 2 and 5. It is parsed as (2 <= n) <= 5 .在 C 中, 2 <= n <= 5不测试n是否在 2 和 5 之间。它被解析为(2 <= n) <= 5 This is evaluated by comparing 2 to n , which produces 0 (if false) or 1 (if true).这是通过比较2n来评估的,这会产生 0(如果为假)或 1(如果为真)。 This result, 0 or 1, is then used in … <= 5 .然后将结果 0 或 1 用于… <= 5 Since 0 and 1 are both less than or equal to 5, the result is always 1 (for true).由于 0 和 1 都小于或等于 5,因此结果始终为 1(为真)。

To test whether n is greater than or equal to 2 and less than or equal to 5, you must write this out explicitly: 2 <= n and n <= 5 , which we join with the “and” operator, && : 2 <= n && n <= 5 .要测试n是否大于或等于 2 且小于或等于 5,您必须明确写出: 2 <= nn <= 5 ,我们使用“与”运算符&&连接它们: 2 <= n && n <= 5

Other Issues其他问题

The proper form for including stdio.h is #include <stdio.h> , not #include stdio.h .包含stdio.h的正确形式是#include <stdio.h> ,而不是#include stdio.h

A proper declaration for main is int main(void) , not int main() . main的正确声明是int main(void) ,而不是int main()

Corrected Program修正程序

A program with these issues corrected is:纠正了这些问题的程序是:

#include <stdio.h>

int main(void)
{
    int n;
    scanf("%d", &n);     //taking input
    if (n % 2 != 0)
    {
        printf ("Weird");     //checking first condition
    }
    else if (2 <= n && n <= 5)
    {               //checking second condition
        printf ("Not Weird");
    }
    else if (6 <= n && n <= 20)
    {               //checking third condition
        printf ("Weird");
    }
    else if (n > 20)
    {               //checking fourth condition
        printf ("Not Weird");
    }
    else
    {
        printf ("Error");
    }
    return 0;
}

There is a typo in the line:该行中有一个错字:

if (n / 2 != 0)

The compiler will not complain, but you will get unexpected results at run time.编译器不会抱怨,但你会在运行时得到意想不到的结果。

Here you meant to check if the remainder of division by 2 is not equal to zero (ie: modulus operator), and not the division by 2. This line should be在这里,您的意思是检查除以 2 的余数是否不等于零(即:模运算符),而不是除以 2。这一行应该是

if (n % 2 != 0)

Second thing: you can't tell C to compare values in ranges like this 2 >= n >= 4 .第二件事:你不能告诉 C 比较像这样的范围内的值2 >= n >= 4 You will have to split the comparison into 2 comparisons.您必须将比较分成 2 个比较。 This line:这一行:

else if (n % 2 == 0 && 2 <= n <= 5)

Should be:应该:

else if (n % 2 == 0 && 2 <= n && n <= 5)

You will need to fix all the lines that have this comparison as well.您还需要修复所有具有此比较的行。

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

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