Introducing Else Statements:
When a condition for an if statement is true, the block of code following it is executed. What about when that condition is false? Normally nothing would happen. With an else statement, an alternate block of code can be executed.
if (num > 10) {
return "Bigger than 10";
} else {
return "10 or Less";
}
Introducing Else If Statements:
If you have multiple conditions that need to be addressed, you can chain if statements together with else if statements.
if (num > 15) {
return "Bigger than 15";
} else if (num < 5) {
return "Smaller than 5";
} else {
return "Between 5 and 15";
}
Logical Order in If Else Statements:
Order is important in if, else if statements.
The function is executed from top to bottom so you will want to be careful of what statement comes first.
Take these two functions as an example.
Here's the first:
function foo(x) {
if (x < 1) {
return "Less than one";
} else if (x < 2) {
return "Less than two";
} else {
return "Greater than or equal to two";
}
}
And the second just switches the order of the statements:
function bar(x) {
if (x < 2) {
return "Less than two";
} else if (x < 1) {
return "Less than one";
} else {
return "Greater than or equal to two";
}
}
While these two functions look nearly identical if we pass a number to both we get different outputs.
foo(0)
bar(0)
foo(0) will return the string Less than one, and bar(0) will return the string Less than two.
Chaining If Else Statements:
if/else statements can be chained together for complex logic. Here is pseudocode of multiple chained if / else if statements:
if (condition1) {
statement1
} else if (condition2) {
statement2
} else if (condition3) {
statement3
. . .
} else {
statementN
}
No comments:
Post a Comment