Page 136 - CITS - Computer Software Application -TT
P. 136

COMPUTER SOFTWARE APPLICATION - CITS



           Example: In this example, we are using the if..else statement to verify whether the given number is positive or
           negative.
           •  Javascript
           let num = -10;

           if (num > 0)
               console.log(“The number is positive.”);
           else
               console.log(“The number is negative”);
           Output

           The number is negative
           Approach 3: Using Switch Statement
           The switch case statement in JavaScript is also used for decision-making purposes. In some cases, using the
           switch case statement is seen to be more convenient than if-else statements.

           Syntax:
           switch (expression) {
               case value1:
                   statement1;

                   break;
               case value2:
                   statement2;
                   break;
               .

               .
               case valueN:
                   statementN;
                   break;
               default:

                   statementDefault;
           }
           Example: In this example, we are using the above-explained approach.
           •  Javascript
           let num = 5;

           switch (num) {
               case 0:
                   console.log(“Number is zero.”);
                   break;

               case 1:
                   console.log(“Nuber is one.”);
                   break;





                                                           123

                             CITS : IT&ITES - Computer  Software Application - Lesson 37 - 46
   131   132   133   134   135   136   137   138   139   140   141