If Else If Else Statement
Syntax 
  if(condition1)
  {
       // execute this block if condition1 is true 
  }
  else if (condition2)
  {
       // execute this block if condition2 is true  
  }
  .
  .
  .
else{
       // execute this block if all above conditions are false 
  }
Conditional  statements are used to perform different actions based on different conditions.
    
  1. if…else if…else statement  are also  known as conditional statements. This simply means that they are able to take  action or decision based on various conditions.
  2. if…else if…else statement  is used to check one blocks of code from many blocks of code. 
  If the condition is true then the  code of block inside if will be  executed. 
  Otherwise the code of block inside else  if will be executed. 
  If all conditions are false then the code of  block inside else will be executed.
  3. if…else  if…else statement  allows the computer to make decisions based  on the values of variables which can take the specified action based on the  input that is given.
  If the decision or condition is true then the  code of block inside if will be  executed.
  If the decision or condition is false  then the code of block inside else if will be executed.
  If all conditions are false then the code of  block inside else will be executed.
You can use this elseif statement many times  as you want.
  You can just use it one time or 40 times, it does  not matter.
Example: 1
<script  type="text/javascript">
  var  name="mila";
  if  (name=="mila")
    {
      alert("hello mila");
     }
  else  if (name=="joe")
     {
      alert("hello joe");
      }
  else
      {
      alert("nobody is here");
      }
  </script>
Example: 2
<script  type="text/javascript">
  var  name="joe";
  if  (name=="mila")
    {
      alert("hello mila");
     }
  else  if (name=="joe")
     {
      alert("hello joe");
      }
  else
      {
      alert("nobody is here");
      }
  </script>
Example: 3
<script  type="text/javascript">
  var  name="bucky";
  if  (name=="mila")
    {
      alert("hello mila");
     }
  else  if (name=="joe")
     {
      alert("hello joe");
      }
  else
      {
      alert("nobody is here");
      }
  </script>
No comments:
Post a Comment