Page 103 - Computer Genius Class 06
P. 103

For Loop
                 The  or loop is used to repeat a se uence a specific num er o  times.
                 Example: To print a statement six times using the ‘for’ loop.
                 Pseudocode:

                 Program Start
                 for value a in a range 1 to 6
                     display ‘I want to play’
                     increase the value of a by 1
                 Program End
                 Example: To print all the odd numbers from a list using for loop.

                 Pseudocode:
                 Program Start
                 list1 = [12, 25, 8, 50, 75, 30]

                 for a in list1
                     if a % 2!= 0
                     print(a, end = ‘ ‘)

                 Program End

                 Nested Loop
                 When there is a loop inside another loop, it is called a nested loop. When one loop is nested within
                 the other, the execution of the entire program depends on the execution of the innermost loop. The
                 outer loop cannot complete its e ecution until the inner loop has finished its e ecution.

                 Entry Criteria

                 Entry criteria is a check for a looping condition, which needs to be true to ensure that the loop runs.
                  ntry criteria is a condition  hich must  e true or must met or must e ist  e ore starting a specific
                 tas .  riteria can  e different  rom program to program as per the re uirement.

                 Let’s understand the concept of entry criteria, by taking an example of starting a bike. To start a
                 bike you need petrol in the fuel tank. If your fuel tank is empty your bike won’t start. So, in this case
                 the entry criterion to start the bike is that the fuel tank should not be empty.
                 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 e it criterion  or the loop.

                  n e it criteria is a condition  hich must  e met  e ore completing a specific tas .  t is a set o
                 conditions that must exist before when you can declare a program to be complete.
                   it criteria differ  rom program to program as per the re uirement.
                 Let’s understand the concept of exit criteria, by taking an example of creating a loop to print numbers
                               th
                 from 1 to 100  exit criteria in this situation, is that the loop should exit the block of code when the
                    th
                 100  num er is printed  else the program  ill enter in an infinite loop.


                                                                                  More on MakeCode Arcade     101
   98   99   100   101   102   103   104   105   106   107   108