Page 25 - Touchcode_C8_Flipbook
P. 25

Coding fact


                   “A good programmer is someone who always looks both ways before crossing a one-way
                   street.”—Doug Linder, Computer Scientist




                 NESTED LOOP

                 Any loop in a program may contain another loop inside it. When there is a loop inside
                 another loop, they are called as nested loop. A nested loop is helpful in case if you are
                 working on multiple conditions. There is no restriction on the number of loops to be nested

                 inside a loop.
                                                                                                     Subject Enrichment


                   Example: To print letters, numbers present in two loops. The outer loop, iterates over
                   the numbers array and the inner loop iterates over the letters array.


                 Pseudocode:
                 Program Start

                 numbers = [1, 2, 3]
                 letters = [a, b, c]
                 for num in numbers

                   for letter in letters
                    print(letter)

                 Program End

                 EXIT CRITERIA
                 A looping condition must get false at a certain point of time, else the block of code will enter
                 in an infinite loop. For this, it is important to define an exit criterion for the loop.

                 An exit criteria is a condition that must be met before completing a specific task. It is a set
                 of conditions that must exist before you declare that a program is complete.


                 Exit criteria differ from program to program as per the requirement.
                 Let’s understand the concept of exit criteria, by taking an example of creating a loop to print

                 numbers from 1 to 1000. Exit criteria in this is that the loop should exit the block of code
                 when the 1000  number is printed, else the program will enter in an infinite loop.
                                 th








                                                                                       Get Creative with Loops   23
   20   21   22   23   24   25   26   27   28   29   30