Page 79 - 2502_Pakistan-kifayat_C-8
P. 79

The above code uses a REPEAT loop to perform an action five times. Inside the loop, the command
                 MOVE (10) steps is executed, meaning the action of moving 10 steps will repeat five times. As a result,
                 the total movement will be 50 steps after the loop completes.

                 Forever Loop

                 The Forever loop is used to repeat a set of actions endlessly until the program is stopped. It is useful for
                 continuous actions like, moving a sprite or checking conditions throughout the game.
                 For example:

                 Start

                 Forever
                     Say "Hello!"
                 End
                 The above code uses a Forever loop to repeat an action endlessly. Inside the loop, the command Say
                 "Hello!" is executed, which means the message "Hello!" will be displayed repeatedly until the program
                 is stopped.


                 Using Loops in Algorithms
                 When you should use which loop:

                    Use Repeat loop when the number is known.

                    Use Forever loop when the task continues without stopping.


                      NESTING IN LOOPS AND CONDITIONS

                 There are situations such as printing multiplication tables from 1 to 5, checking if students in each row
                 have brought their ID cards, or evaluating marks for multiple subjects to determine if a student has
                 passed all subjects. These require the use of nested loops and conditions to be solved effectively.

                 What are Nested Loops?

                 Nested loops are loops placed inside another loop. This means one loop runs within the body of another
                 loop.

                 For example:


                 Start
                 Repeat (3)

                     Repeat (2)

                         Print "Hello!"
                 End
                 The code uses a nested Repeat loop where the outer loop runs 3 times and the inner loop runs 2 times
                 for each outer loop repetition. Inside the inner loop, the command Print "Hello!" is executed. As a
                 result, the word "Hello!" will be displayed a total of 6 times.




                                                                                                          #Nesting  77
   74   75   76   77   78   79   80   81   82   83   84