unmultimedio
unmultimedio

Reputation: 1244

Difference between else if and else{if}

is there an actual difference between these two?

First:

if(condition1){
    // code 1
}else if(condition2){
    // code 2
}else if(condition3){
    // code 3
}else if(condition4){
    // code 4
}else if(condition5){
    // code 5
}else{
    // code 6
}

Second

if(condition1){
    // code 1
}else{
    if(condition2){
        // code 2
    }else{
        if(condition3){
            // code 3
        }else{
            if(condition4){
                // code 4
            }else{
                if(condition5){
                    // code 5
                }else{
                    // code 6
                }
            }
        }
    }
}

I'm asking as far as performance or better practices or even readability.

BTW: I know there is the switch sentence, but I'm just curious. :)

Upvotes: 1

Views: 72

Answers (2)

Jan Berktold
Jan Berktold

Reputation: 951

One can not say that using one way over the other one is more efficient in every single language.

However, in the case that there are any differences in the language you are using, then those differences should be unnoticeable.

Upvotes: 0

Tony Hopkinson
Tony Hopkinson

Reputation: 20320

The braces mark a compound statement or block, i.e multiple statements. In your example there's only one statement so the braces can be skipped, that's it. Difference has no practical value except to those who have big arguments over indentation.

Upvotes: 3

Related Questions