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

COMPUTER SOFTWARE APPLICATION - CITS



               case 2:

                   console.log(“Number is two.”);
                   break;
               default:
                   console.log(“Number is greater than 2.”);
           };
           Output
           Number is greater than 2.
           Approach 4: Using the Ternary Operator (Conditional Operator)
           The conditional operator, also referred to as the ternary operator (?:), is a shortcut for expressing conditional
           statements in JavaScript.
           Syntax:
           condition ? value if true : value if false

           Example: In this example, we are using the ternary operator to find whether the given number is positive or
           negative.
           •  Javascript

           let num = 10;
           let result = num >= 0 ? “Positive” : “Negative”;
           console.log(`The number is ${result}.`);
           Output
           The number is Positive.
           Approach 5: Using For loop
           In this approach, we are using for loop in which the execution of a set of instructions repeatedly until some
           condition evaluates and becomes false
           Syntax:
           for (statement 1; statement 2; statement 3) {
               // Code here . . .
           }
           Example: In this example, we are using Iterative Statement as a for loop, in which we find the even number
           between 0 to 10.
           •  Javascript
           for (let i = 0; i <= 10; i++) {
             if (i % 2 === 0) {
               console.log(i);
             }
           };
           Output:
           0
           2
           4
           6

           8
           10



                                                           124

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