它总是提示"code will never be executed"

It always prompts "code will never be executed"

本文关键字:never be executed will code 提示      更新时间:2023-10-16

这是我的C++代码:

#include <stdio.h>
int main ()
{
    int n, ip1, ip2, ip3, ip4, port;
    unsigned ip;
    char str [80];
    printf ("Enter IPv4 address: ");
    scanf ("%79s",str);
    n = sscanf(str, "%3d.%3d.%3d.%3d:%5d", &ip1, &ip2, &ip3, &ip4, &port);
    printf ("Content length is %drn", n);
    if (n != 4 || n != 5) {
        ip = 0;
        port = 161;
    }
    else if (n==4) {
        port = 161;
    }
    ip =(ip1 << 24) + (ip2 << 16) + (ip3 << 8) + ip4;
    printf ("ip addr is %d , port is %d rn", ip, port);
}

但xcode总是提示我:

else if (n==4) 

永远不会被处决,而且确实发生了。条件应该如何?

请注意,(n != 4 || n != 5) = true

您可能想要if (n != 4 && n != 5),因为如果使用||,那么n要么不是4,要么不是5。