Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use of "else if" in c++

I have two questions - (I)

code-fragment-1

if(<condition-statement>){
}

else if(<condition-statement-2>){
    //statements-1
}
//statements-2

code-fragment-2

if(<condition-statement>){
}

else{
    if(<condition-statement-2>){
        //statements-1
    }
    //statements-2
}

Are the above two code fragments same?

(II) when are else ifs (in C++) used?

like image 644
SegFault Avatar asked Oct 06 '12 07:10

SegFault


1 Answers

The only difference is in example 1 your Statement2 will get executed regardless of the conditions you check. In example 2, Statement2 will only get executed if your if condition is false. Other than that, they're basically the same.

like image 167
Adam Plocher Avatar answered Oct 23 '22 14:10

Adam Plocher