Page 57 - Digicode_AI_class_8
P. 57

Critical Thinking
                   code Quest


                 Write the result for the following conditions if num1 is 200 and num2 is 400:

                  If (num1 > 250) OR              If (num1 > 250)                 If (num1 < 250)
                  (num2 > 250)                    AND
                                                                                  Then
                  Then                            (num2 > 250)
                                                                                     Write “TEA”
                      Write “TEA”                 Then
                                                                                  Else
                  Else                                Write “TEA”
                                                                                     Write “COFFEE”
                      Write “COFFEE”              Else
                                                      Write “COFFEE”

                 1.                              2.                              3.






            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:

            Robby has to paint these blank boxes to make a letter ‘U’










            The program will be

                    Move 1 down                                      Repeat 4 times
                    Move 1 down                                      (Move 1 down)
                    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)
                    Move 1 up
                    Move 1 up

                          Program without a loop                            Program with a loop


                                                                                 Algorithmic Intelligence  55
   52   53   54   55   56   57   58   59   60   61   62