switch case javascript

 

Switch Case Statement 



1.Introduction of Switch case

2.Switch case components 

3.Syntax of Switch case

4.Example of Switch Case 

Introduction of Switch case

        If..Else statement is branching statement here one or more condition but not multiway decision so in JavaScript a built in multiway decision  statement  known as  Switch. Switch statement is decision making statement.

Switch case components

The switch statement has following components-

        1.Initial switch statement with syntax switch(value to test)

        2.The braces { and } to enclose switch statement

        3.one or more case values i.e.case value:statement

    If the value of case matches with the case value by switch statement then the statements after case are executed.

        4.break statement is used to end each case.

        5.default statement which specifies the statement to be executed if none of the case matches with switch statement.

Syntax of Switch case

        switch(condition)

    {

        case value 1:statement1/s;

                               break;

        case value 2:statement2/s;

                            break;

        case value 3:statement3/s;

                        break;

                .

                .

        case value n:statement n/s;

                        break;

       default :default statement;

    } 


Click Here Video 

Example of Switch Case 

    <!DOCTYPE html>

    <head>

    <title>Javascript Program</title>

    </head>

    <body>

    <h1> use of switch case </h1>

   <script type="text/javascript">

    var choice,n1,n2,m,d;

    n1=prompt("Enter First Number");

    n2=prompt("Enter Second Number");

    choice=parseInt(prompt("Enter Choice"));

    switch(choice)

    {

        case 1: m=n1*n2;

                   document.write("Multiplictaion="+m);

        break;

        case 2: d=n1/n2;

    document.write("Division="+d);                  

        break;

    default:document.write("Enter value 1 and 2 only otherwise Invalid Number");

    }

    </script>

    </body></html>

Output-

1.Enter First Number


Img-01
2.Enter Second Number


Img-02




3.Enter Choice


Img-03


4.Output


Img-04

Previous
Next Post »

Please Comment and Share. ConversionConversion EmoticonEmoticon