It tells the computer of actions to take in both the scenarios. i.e. What to do if condition is true and what to do if it is False. If a certain condition is true, execute a block of code, if false- execute another block of code mentioned.
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. Example below will clarify it, and also see the note below.
If statement SYNTAX
If (Condition)
{
Block of code to be executed, if condition is True
}
Else
{
Block of code to be executed, if condition is True
}
If the condition however 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 executes the block of code in the ELSE part and then moves to the next lines in the html document.
Example
We have a site for displaying semester results of students when they log in to the site. What instructions to give to the student incase she has passed and what in other scenarios.
Possible Scenarios
Passed
Failed
If (status = passed)
{
"Congratulations, you have passed and been promoted to the next level "
}
Else
{
"Sorry. You could not pass the exam. Contact the administration department for further info"
}
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 ELSE Ifwhich we will see in next lesson.
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. Example below will clarify it, and also see the note below.
If statement SYNTAX
If (Condition)
{
Block of code to be executed, if condition is True
}
Else
{
Block of code to be executed, if condition is True
}
If the condition however 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 executes the block of code in the ELSE part and then moves to the next lines in the html document.
Example
We have a site for displaying semester results of students when they log in to the site. What instructions to give to the student incase she has passed and what in other scenarios.
Possible Scenarios
Passed
Failed
If (status = passed)
{
"Congratulations, you have passed and been promoted to the next level "
}
Else
{
"Sorry. You could not pass the exam. Contact the administration department for further info"
}
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 ELSE Ifwhich we will see in next lesson.
Aucun commentaire:
Enregistrer un commentaire