else blocks execute only if the preceeding if block did not execute.
1 2 3 4 5 6 7 8
if( foo )
{
// this executes only if foo is true
}
else
{
// this executes only if foo is false
}
putting another 'if' after the else is like putting it inside the else block:
1 2 3 4 5 6 7 8 9 10
if( foo ) { ... }
elseif( bar ) { ... }
// is conceptually the same as
if( foo ) { ... }
else
{
if( bar ) { ... }
}
As such, chaining together multiple else-if's makes it so that each block is dependent on all previous conditions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
if( foo )
{
// executes only if foo is true
}
elseif( bar )
{
// executes only if foo is false and bar is true
}
elseif( baz )
{
// executes only if foo is false, bar is false, and baz is true
}
else
{
// executes only if foo, bar, and baz are all false
}
Independent if statements do not depend on previous if statements:
1 2 3 4 5 6 7 8
if( foo )
{
// executes only if foo is true
}
if( bar ) // <- no else
{
// executes only if bar is true. Whether foo was true or not does not matter
}