Page 370 - Webapplication11_C11_Flipbook
P. 370

2.  Write JavaScript code for the following:
                    •   Accept salary and grade from the user. Based on the grade add the bonus to the salary and display net
                       salary.
                          Grade                                       Bonus
                          A                                           30% of salary
                          B                                           35% of salary
                          C                                           20% of salary
                          D                                           15% of salary
                    •   Write a program using the switch statement to print the names of class teacher of all grades from 1 to 10. Hint:
                       let the grade be a variable. depending on its value, print the names of the class teacher.   [CBSE Handbook]





                 4.23 LOOPING STRUCTURE IN JAVASCRIPT

              Sometimes, you need to repeat a task multiple times or you may need to repeat the task either a certain number of
              times or until a condition is satisfied. In JavaScript, the statements that are used to repeat a set of instructions are
              called iterative or looping statements or simply loops. Loops are very useful and necessary for developing applications.

              JavaScript provides four types of loop:

                                                             Types of Loops



                                           for          for...in        while        do...while


              The for loop

              The for statement or loop is used to repeat an instruction or a set of instructions for a fixed number of times. The
              syntax to use the for loop is as follows:
              for (initialisation_expression; conditional_expression; update_ expression) {

              // loop body
              }
              Here,

              Ð Ðinitialisation_expression: In this expression, a variable is initialised which is also known as loop control variable.
                 This expression is executed only once in a loop.

              Ð Ðconditional_expression:  This  expression  contains  a  logical  condition  which  returns  either  True  of  False.  If  the
                 condition evaluates to True, the statements written inside the loop body will execute. If the condition evaluates to
                 False, the statements written inside the loop body will be skipped. Loop will terminate once the condition becomes
                 False.

              Ð Ðupdate_expression: This expression is executed after every pass of the loop. A pass of a loop is known as an iteration.
                 In this expression, the loop control variable is updated by increasing or decreasing its value.

              Ð ÐLoop body: This section contains the statements that are to be executed repeatedly enclosed within the curly
                 braces.
              Ensure that every expression should be terminated with a semicolon in the for loop, otherwise an error will occur.

                368     Touchpad Web Applications-XI
   365   366   367   368   369   370   371   372   373   374   375