Sunday 22 December 2013

Javascript : Conditional Statement Part-2

Javascript(JS):

Syntax:

Syntax of the switch statement is to evaluate and several different
statements to execute based on the value of the expression. The interpreter
checks each case against the value of the expression until a match is found.
If nothing matches, a default condition will be used.

switch (expression)
{
  case condition 1: statement(s)
                    break;
  case condition 2: statement(s)
                    break;
   ...
  case condition n: statement(s)
                    break;
  default: statement(s)
}

Example:

<script type="text/javascript">

var grade='A';
document.write("switch Section<br />");
switch (grade)
{
  case 'A': document.write("Excellent<br />");
            break;
  case 'B': document.write("Very good<br />");
            break;
  case 'C': document.write("good<br />");
            break;
  case 'D': document.write("poor<br />");
            break;
  case 'F': document.write("Failed<br />");
            break;
  default:  document.write("no grade<br />")
}
document.write("Exit switch block");

</script>


Note : Break keyword is used to comes out of looping statment
           When the condition satisfied .. break keyword helps to
           poped out of that statement

No comments:

Post a Comment