ADONIS
Reference Manual
×
Menu
Index
If...Else Statements
 
Conditional Statements:
Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this. In JavaScript we have the following conditional statements:
1. Use if to specify a block of code to be executed, if a specified condition is true
2. Use else to specify a block of code to be executed, if the same condition is false
3. Use else if to specify a new condition to test, if the first condition is false
4. Use switch to specify many alternative blocks of code to be executed
 
Syntax:
Use the if statement to specify a block of JavaScript code to be executed if a condition is true.
if (condition) {
    block of code to be executed if the condition is true
}
Use the else statement to specify a block of code to be executed if the condition is false.
if (condition) {
    block of code to be executed if the condition is true
} else {
    block of code to be executed if the condition is false
}
Use the else if statement to specify a new condition if the first condition is false.
if (condition1) {
    block of code to be executed if condition1 is true
} else if (condition2) {
    block of code to be executed if the condition1 is false and condition2 is true
} else {
    block of code to be executed if the condition1 is false and condition2 is false
}
 
Example:
If ID is less than 2, create a "Elastic" model, if not, but time is less than 4, create a "Mohr-Coulomb" model, otherwise a "Hoek-Brown":
if (ID < 2) {
    model = "Elastic";
} else if (time < 4) {
    model = "Mohr-Coulomb";
} else {
    model = "Hoek-Brown";
}
The result of model will be:
Mohr-Coulomb
The help manual was created with Dr.Explain