Page 99 - PortGPT_V2.1_C8_Flipbook
P. 99

7                          Loops in JavaScript

















                     Let’s surf
                         The for Statement                          The while Statement
                          The do-while Statement                    Nested Loop
                          Infinite Loop                             Jump Statements in JavaScript
                          Some More Programs


                  Let’s PLug-in                                                                         Critical Thinking


                    State whether these statements are true or false.

                    1.   JavaScript does not support conditional statements. __________

                    2.   We cannot use if statement inside other if statement. __________
                    3.   We can check multiple conditions 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 three types of loops—for,
                 while and do-while.

                 In this chapter, you will learn about loops in JavaScript. Next, you will learn about nested loops in
                 JavaScript. At the end of this chapter, you will learn about jump statements in JavaScript.


                        THE FOR STATEMENT

                 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
                 }



                                                                                               Loops in JavaScript  97
   94   95   96   97   98   99   100   101   102   103   104