Page 128 - CG_v2.1_flipBooks_class_5
P. 128

Loops

                  Loops are command blocks in AI connect that can put sets of code in repetitive cycles in

                  order to reduce the steps of the program. There are several types of loop blocks available
                  that are all designed for different purposes. The following are the main types of loop blocks
                  that we can use in AI Connect:

                  For Loop
                  It can repeat a set of blocks for a certain number

                  of times and perform an action on it each time it is
                  repeated.
                  This block is very useful for performing operations on
                  multiple items of a list and printing out the results.

                  Range block

                  The ‘Range’ block can be used with the ‘for each’ block to set the maximum
                  number of times it will repeat its actions.



                                                                 Notes

                             In range block, the input can only be integers but not a negative integer. As
                             the count always starts from 0 and not 1.


                  Repeat block

                  ‘Repeat’ command block simply repeats the blocks attached to it

                  for a certain number of times.
                  It is used to save time and effort as we only need to type the
                  commands once and the block will repeat them automatically.

                  Repeat while

                  This block also repeats a set of commands in a code but only till a
                  specified condition is met.

                  As long as the condition stays true, the code keeps repeating.
                  Uses of Loop Blocks

                  ‘Loop’ command blocks can be used to print out many useful things that need repetition like
                  multiplication table, timers, data filling, etc.

                  Program to Print Multiplication Table of 7


                  To print multiplication table of 7 in AI Connect, do the following coding and click on Run
                  button:



                      126    Computer Genius (V2.1)-V
   123   124   125   126   127   128   129   130   131   132   133