Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between nested if and &&?

Tags:

c

if-statement

What are the differences between writing:

if(condition1 && condition2){
    //some code
}

And:

if(condition1){
    if(condition2){
        //some code
    }
}

If there are any, which one is better?

like image 918
2013Asker Avatar asked Dec 01 '22 03:12

2013Asker


1 Answers

The differences are mainly in readability and maintenance.

Concatenation of 2 logical conditions should usually imply that there is a semantic relation between them.

Another thing to consider is the scoping. The nested if gives you additional flexibility in this area.

like image 53
SomeWittyUsername Avatar answered Dec 05 '22 01:12

SomeWittyUsername