Page 199 - CA_Blue( J )_Class10
P. 199

09


                                                                             NESTED LOOP
















                           Learning Objectives



                     9.1 Nested for Loop                            9.2 Nested while Loop
                     9.3 Nested do-while Loop                       9.4 Using the break Statement in Nested Loop
                     9.5 Using the continue Statement in Nested Loop   9.6 Pattern Programs using Nested Loop





                 In the previous chapter, we studied looping statements. As you know that a loop is used to execute a set of statements
                 repeatedly until a condition evaluates to true. But sometime you may need to execute a loop multiple times. To solve
                 this type of problem, you need to write a loop inside another loop. A loop inside another loop is called a nested loop.
                 All the three loops (for, while and do-while) provided by Java can be used as a nested loop. Syntax to use nested loop is:
                    Outer loop structure starting
                    {
                        Inner loop structure starting
                        {
                           Statements inside inner loop
                        }
                        Statements inside outer loop
                    }
                 When two loops are nested, the inner loop depends on the outer loop for its execution which means if the test condition
                 provided in the outer loop does not evaluate to true, then the inner loop will not execute. If the test condition provided
                 in the outer loop evaluates to true, then the test condition provided in the inner will be checked and if it is evaluated
                 to true, then the inner loop will execute until the test condition becomes false. For each iteration of the outer loop, the
                 inner loop will execute until its test condition becomes false. Let us study nested loops in detail.


                     9.1 NESTED FOR LOOP
                 A nested loop in Java refers to a loop that is contained within another loop. The inner loop executes entirely for each
                 iteration of the outer loop. This type of loop is useful when you need to work with multidimensional data or perform
                 repetitive tasks for a number of elements across multiple dimensions (such as rows and columns in a matrix or table).











                                                                                                                       197
                                                                                                         Nested Loop   197
   194   195   196   197   198   199   200   201   202   203   204