嵌套 if 语句

发布于 2024-11-28 06:14:16 字数 1431 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

困倦 2024-12-05 06:14:16

编辑:感谢您澄清问题。现在的问题实际上是一种风格,归结为什么是最具可读性的。一般来说,当您拥有真正的多路条件时,后者更好,这意味着在代码中的某个时刻,存在三种可能的条件,它们都同样可能且同样合理。当语义上 cond1 是真实驱动程序时,前者可能更清晰,而 cond2 是一种从属条件,人们只有在 cond1 为真时才会考虑。

旧答案:

这是对该问题的一种解释(“嵌套在 then 内部”):

if (cond1) {
    if (cond2) {
        A
    }
} else {
    B
}

正如 Justin 指出的那样,这应该写成

if (cond1 && cond2) {
    A
} else {
    B
}

除非有代码需要位于if cond1 部分不在 if cond2 的范围内。

您的问题的另一种解释(“嵌套在 else 内部”) ”)是:

if (cond1) {
    A
} else {
    if (cond2) {
        B
    }
}

在这种情况下你应该重写为

if (cond1) {
    A
} else if (cond2) {
    B
}

因为它更好地表达了多路条件的思想。同样,如果有一些内容不属于 cond1 的范围但不属于 cond2 的范围,那么您需要嵌套。

希望有帮助。

EDIT: Thanks for clarifying the question. The question is now really one of style and comes down to what is most readable. Generally speaking the latter is nicer when you have a true multi-way conditional, meaning at some point in the code there are three possible conditions all equally likely and equally sensible. The former is cleaner perhaps when semantically cond1 is the real driver, and cond2 is a kind of subordinate condition that people only think about when cond1 is true.

OLD ANSWER:

Here is one interpretation of the question ("nesting inside of then"):

if (cond1) {
    if (cond2) {
        A
    }
} else {
    B
}

This should be written, as Justin points out, as

if (cond1 && cond2) {
    A
} else {
    B
}

unless there is code that needs to be in the if cond1 part that is not in the scope of the if cond2.

Another interpretation of your question ("nesting inside of else") is:

if (cond1) {
    A
} else {
    if (cond2) {
        B
    }
}

In this case you should rewrite as

if (cond1) {
    A
} else if (cond2) {
    B
}

Because it better expresses the idea of the multiway conditional. Again if there is something that belongs in the scope of not cond1 but not in the scope of the cond2, then you need to nest.

Hope that helps.

美人迟暮 2024-12-05 06:14:16

事实上,你的例子并不匹配。我会将第一个重写为:

if(cond && cond2)
{

}
else
{

}

像您的第一个示例那样嵌套 if 语句的唯一一次是:

if(cond)
{
    if(cond2)
    {
        // Stuff that requires both conditions to be true.
    }

    // Stuff that requires only the first condition to be true.
}
else
{

}

Actually, your examples don't match. I would rewrite the first to be:

if(cond && cond2)
{

}
else
{

}

The only time I would nest if statements like your first example would be:

if(cond)
{
    if(cond2)
    {
        // Stuff that requires both conditions to be true.
    }

    // Stuff that requires only the first condition to be true.
}
else
{

}
夏了南城 2024-12-05 06:14:16

编辑:在这种情况下,我认为两者都不一定更好。在我看来,(b) 更容易阅读并遵循逻辑。不管是哪一个都可以!

(a) 中的 else 是与第一个 if 语句还是第二个 if 语句一起使用?顺便说一句,您缩进的方式,我假设它与第一个 if 语句一致,但如果是这样,那么您的两个示例在逻辑上并不相同。

Edit: In that case, I don't think either is necessarily better. In my opinion, (b) is much easier to read and follow logically. Either one will do though!

Does the else in (a) go with the first if statement or the second? By the way you indented, I would assume that it goes with the first if statement, but if that is the case, then your two examples aren't the same logically.

傲娇萝莉攻 2024-12-05 06:14:16

例子 :

<?php 
    if(find_user($email)){
        if(check($email,$password)){
            $_SESSION['email'] = $email;
            setcookie("name",$_SESSION['name'],time() +40);
            header("Location: index.php");
        }else{
            echo "incorrect email or password";
        }
    }else{
        echo "username not found !";
    }
    ?>
    
    <?php 
    if(find_user($email)){
        if(find_user($name)){
            if(check($password,$passwordcp)){
           $f = "readme.txt";
           $file = fopen($f,"a+");
           fwrite($file,"\n".$email.","$password.",".$name);
           fclose($file);
            }else{
            echo "Password contains invalid characters";
        }else{
            echo "username already exist !";
        }
    }else{
        echo "email already exist !";
    }
    ?>

Example :

<?php 
    if(find_user($email)){
        if(check($email,$password)){
            $_SESSION['email'] = $email;
            setcookie("name",$_SESSION['name'],time() +40);
            header("Location: index.php");
        }else{
            echo "incorrect email or password";
        }
    }else{
        echo "username not found !";
    }
    ?>
    
    <?php 
    if(find_user($email)){
        if(find_user($name)){
            if(check($password,$passwordcp)){
           $f = "readme.txt";
           $file = fopen($f,"a+");
           fwrite($file,"\n".$email.","$password.",".$name);
           fclose($file);
            }else{
            echo "Password contains invalid characters";
        }else{
            echo "username already exist !";
        }
    }else{
        echo "email already exist !";
    }
    ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文