+ 1
explain this in brief
The final else statement "ends" the else if statement and should be always written after the if and else if statements.
1 Antwort
0
Whenever using the "if" statement, you need to end with an else unless you want it to do nothing if a statement is not true (or false for that matter) e.g:
if (1 == 2)
{
Console.log("1 == 2")
} else
{
Console.log("1 != 2")
}
Example of an if-else if- else:
if (condition)
{
//first if statement
} else if(condition)
{
//second if statement
} else {
//this code will run if neither of the conditions are true
}
Whenever using a lot of "else if" statements after one another you might want to consider using the "switch" statement.
Hope this helps