Java Reference
In-Depth Information
else and else if
Imagine a situation where you want some code to execute if a certain condition is true and some
other code to execute if it is false. You can achieve this by having two if statements, as shown in the
following example:
if (myAge >= 0 && myAge <= 10) {
document.write("myAge is between 0 and 10");
}
if ( !(myAge >= 0 && myAge <= 10) ) {
document.write("myAge is NOT between 0 and 10");
}
The first if statement tests whether myAge is between 0 and 10 , and the second for the situation
where myAge is not between 0 and 10 . However, JavaScript provides an easier way of achieving
this: with an else statement. Again, the use of the word else is similar to its use in the English
language. You might say, “If it is raining, I will take an umbrella; otherwise I will take a sun hat.”
In JavaScript you can say if the condition is true , then execute one block of code; else execute
an alternative block. Rewriting the preceding code using this technique, you would have the
following:
if (myAge >= 0 && myAge <= 10) {
document.write("myAge is between 0 and 10");
} else {
document.write("myAge is NOT between 0 and 10");
}
Writing the code like this makes it simpler and therefore easier to read. Plus it also saves JavaScript
from testing a condition to which you already know the answer.
You could also include another if statement with the else statement. For example:
if (myAge >= 0 && myAge <= 10) {
document.write("myAge is between 0 and 10");
} else if ( (myAge >= 30 && myAge <= 39) || (myAge >= 80 && myAge <= 89) ){
document.write("myAge is between 30 and 39 " +
"or myAge is between 80 and 89");
} else {
document.write("myAge is NOT between 0 and 10, " +
"nor is it between 30 and 39, nor " +
"is it between 80 and 89");
}
The first if statement checks whether myAge is between 0 and 10 and executes some code if that's
true . If it's false , an else if statement checks if myAge is between 30 and 39 or 80 and 89 , and
executes some other code if either of those conditions is true . Failing that, you have a final else
statement, which catches the situation in which the value of myAge did not trigger true in any of the
earlier if conditions.
When using if and else if , you need to be extra careful with your curly braces to ensure that the
if and else if statements start and stop where you expect, and you don't end up with an else that
 
Search WWH ::




Custom Search