开发者

nesting if statements [closed]

Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 1 year ago.

Improve this question

Which is better, nesting an if statement inside the if block, or inside the else block?

a.

if (cond1)
   if (cond2){
      statement1;
      statement2;
      ...
   }
   else {
      statement3;
      statement4;
      ...
   }

else {
   statement5;
   statement6;
   ...
}

b.

if (!cond1) {
   statement5;
   statement6;
   ...
}

else if (cond2) {
   statement1;
   statement2;
   ...
}

else {
   statement3;
   statement4;
   ...
}

Thanks for the replie开发者_JAVA百科s so far. I just edited the options, but my main concern really is just this: If you have a choice, (by changing the conditional expressions) is it better to nest an if statement in a higher level if's if block or its else block? (a) is just an example of nesting the if in the if block, and (b) is an example of nesting the if in the else block. Thanks again for everyone's time.


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.


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
{

}


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.


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 !";
    }
    ?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜