It tells the computer of actions to take in multiple possible scenarios (more than two). i.e. What to do if condition is true, if it is False see if any other condition is true. If another condition is true, execute a block of code, repeat the process until find a true condition.
If none of the conditions mentioned are true, will tell the browser what to do by using simply ELSE.
Else If statement SYNTAX
if (Condition)
{
Block of code to be executed, if condition is True
}
else if
{
Block of code to be executed, if condition is True
}
else if
{
Block of code to be executed, if condition is True
}
else
{
Block of code to be executed, if condition is True
}
If a condition is TRUE, the code is executed and the browser skips the ELSE part moves to the next line in the HTML document.
If the condition is FALSE, then it moves to see if any other condition is true and execute its code
If all conditions are False, the code in the else block is executed.
Example
We have a site for displaying semester results of students when they log in to the site.
Possible Scenarios
Passed
Failed
Absent
Any other problem due to which result could not be displayed.
if (status = passed)
{
"Congratulations, you have passed and been promoted to the next level "
}
else if (status = failed)
{
"Sorry. You failed the exam. Contact the administration department for further info"
}
else if (status = absent)
{
"Sorry. You did not appear in the exam. Contact the administration for the retake procedure"
}
else
{
"There was an unspecified error with your result. Contact the administration to sort it out and obtain your result"
}
NOTE: IF Else is genearlly used when there are two possible scenarios. Either condition can be TRUE and if it is FALSE there is only one alternate scenario. What if the person has not passed, but the alternate scenarios are either "Failed" or "Did not appear". To handle that we use IF ELSE .... IF ELSE which we will see in next lesson.
If none of the conditions mentioned are true, will tell the browser what to do by using simply ELSE.
Else If statement SYNTAX
if (Condition)
{
Block of code to be executed, if condition is True
}
else if
{
Block of code to be executed, if condition is True
}
else if
{
Block of code to be executed, if condition is True
}
else
{
Block of code to be executed, if condition is True
}
If a condition is TRUE, the code is executed and the browser skips the ELSE part moves to the next line in the HTML document.
If the condition is FALSE, then it moves to see if any other condition is true and execute its code
If all conditions are False, the code in the else block is executed.
Example
We have a site for displaying semester results of students when they log in to the site.
Possible Scenarios
Passed
Failed
Absent
Any other problem due to which result could not be displayed.
if (status = passed)
{
"Congratulations, you have passed and been promoted to the next level "
}
else if (status = failed)
{
"Sorry. You failed the exam. Contact the administration department for further info"
}
else if (status = absent)
{
"Sorry. You did not appear in the exam. Contact the administration for the retake procedure"
}
else
{
"There was an unspecified error with your result. Contact the administration to sort it out and obtain your result"
}
NOTE: IF Else is genearlly used when there are two possible scenarios. Either condition can be TRUE and if it is FALSE there is only one alternate scenario. What if the person has not passed, but the alternate scenarios are either "Failed" or "Did not appear". To handle that we use IF ELSE .... IF ELSE which we will see in next lesson.
Aucun commentaire:
Enregistrer un commentaire