Page 99 - TP-Play_V-2.0_Book-8
P. 99

LOOPS IN A PROGRAM


            A loop is used to execute instructions or a block of code multiple times, without writing it
            repeatedly. A sequence of instructions when repeated for a fixed number of times or until

            a condition is true is called a loop. When a loop repeats forever (endlessly), it is called an
            infinite loop. There are two types of loops.
                 Counting loops: These repeat a certain number of times.

                 Conditional loops: These repeat until a certain condition is reached which means they
               keep going until some condition remains true.

            Look at this example:
                                                      Move 1 down                    Repeat 4 times
            Robby has to paint these blank            Move 1 down                    (Move 1 down)
            boxes to make a letter ‘U’                Move 1 down
                                                      Move 1 down
                                                      Move 1 right                   Repeat 3 times
                                                      Move 1 right                   (Move 1 right )
                                                      Move 1 right
                                                      Move 1 up                      Repeat 4 times
                                                      Move 1 up                      (Move 1 up)
            The program will be
                                                      Move 1 up
                                                      Move 1 up                                                          Computational Thinking


            A.  Write your answer.                     Program without a loop           Program with a loop


                 If (you are a girl) AND (you are born in April)
                 Then
                        Write “Both”
                 Else
                        Write “None”
            B.  Write the output of the following programs:


                1.                                     Start
                     Repeat 6 times
                     (
                       Put a cross
                       Move 1 step right
                       Put a tick
                       Move 1 step down
                     )

                2.
                     Repeat 3 times
                     (
                       Colour Yellow
                       Skip One Triangle
                     )                                              Start


                                                               The CT Corner! (Algorithmic Intelligence)     99
   94   95   96   97   98   99   100   101   102   103   104