0
whats the need of else if statement?? even if is the the condition, if the statement is not true it will will be else part works
2 odpowiedzi
+ 2
sometimes you dont have just 2 options...example:
if(a<10)
{}
else if(a>10 && a<100)
{}
else
{}
+ 2
"Else if" can be used after an if statement.
If you want to have two different options (different if statements), then you can use two if statements OR an if statement and an else if statement:
if (...)
{
/// Do something
}
if (...)
{
/// Do something else
}
or:
if (...)
{
/// Do something
} else if (...)
{
/// Do something else
}
The difference between these two is that the first example checks both statements, while the second example checks the "if statement" first.
It will only check the "else if statement" if the first "if statement" is false.
You can use the second example if you have code that only needs to be checked if it's necessary.
You can also have an "else statement" in there. That statement is only checked if the statements before that are false:
if (...)
{
/// Do something
} else if (...)
{
/// Do something else
} else {
/// Do something else 2
}