Page 57 - Dig_CodeAI_V2.1_Class_8
P. 57

Critical Thinking
                       code Quest


                      Write the result for the following conditions:

                                       day ="Sunday"                   height_inch=46
                                       IF day = "Saturday"             ticket ="no"
                                       OR day= "Sunday"                IF height_inch > 40
                                       THEN                            AND ticket = "yes"
                                            PRINT "It's a              THEN
                                            holiday"                        PRINT "Entry
                                       ELSE                                 allowed"
                                            PRINT "It's not            ELSE
                                            a holiday"                      PRINT "Entry
                                                                            not allowed"


                                      1.                              2.




                 Loops in a Program
                 A loop allows a set of instructions or a block of code to be executed repeatedly. It helps in automating
                 repetitive tasks and makes the code more efficient by avoiding the need to write the same instructions
                 multiple times. When a loop repeats forever (endlessly), it is called an infinite loop. There are two
                 types of loops.
                   Counting loops: These repeat a block of code a certain number of times.
                   Conditional loops: These repeat until a certain condition is reached, which means they keep
                   going until the condition remains true.

                 For example:
                 Robby has to paint these blank boxes to make a letter ‘U’
                                          Start                          Start










                 The program will be

                         Move 1 down and Fill Colour                      Repeat 4 times
                         Move 1 down and Fill Colour                      (Move 1 down and Fill Colour)
                         Move 1 down and Fill Colour
                         Move 1 down and Fill Colour                      Repeat 2 times
                         Move 1 right and Fill Colour                     (Move 1 right and Fill Colour)
                         Move 1 right and Fill Colour
                         Move 1 up and Fill Colour                        Repeat 3 times
                         Move 1 up and Fill Colour                        (Move 1 up and Fill Colour)
                         Move 1 up and Fill Colour


                               Program without a loop                            Program with a loop




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