Page 64 - Dig_CodeAI_V2.1_Class_8
P. 64

In the previous class, you have learnt what are variables, data types in programming, sequence,
                  loop, bug, event and collections. In this chapter, you will learn about loops and functions.

                  What are Control Structures?

                  A control structure in programming is a fundamental construct that directs the flow of execution
                  within a program based on certain conditions or criteria. So, the control structure is a basic decision-
                  making process in computing. There are three main types of control structures:

                  •   Sequential: Statements in a program are executed sequentially or step-by-step in an order in
                     which the instructions are written.
                  •   Selection/Conditional: It is used to test a condition in a program. It takes decision to execute
                     one statement/operation over another statement/operation depending on the condition.
                  •   Iteration: It executes set of statements for a certain number of times till the given condition is
                     true. For example: Loops.
                  On the basis of the above three control structures, programming has derived many types of control
                  statements to suit different needs.

                  Loops

                  Loops are  fundamental  programming  constructs  that  allow  you  to  repeat  a  set  of instructions
                  multiple times. In MakeCode Arcade, loops are especially useful for tasks like animating sprites,
                  creating repeated patterns, or controlling different elements.

                  Benefits

                     Loops let you repeat actions without writing the same code again and again.

                     They make your code shorter and easier to understand.
                     Loops are great for doing things many times, like moving a character.
                     Changing one loop can update lots of parts of your game at once.

                     Loops make your game more fun by adding repeating animations and patterns
                  For example:  You have five plants lined up in your garden, and you want to water all. Instead of
                  watering each plant individually and keeping track of which ones you've already watered, you can
                  think of using a loop:

                  1. Start Watering: You begin at the first plant and water it.

                  2. Move to the Next Plant: After watering the first plant, you move to the second plant.
                  3.  Repeat the Process: You continue watering each plant one by one, moving to the next plant after
                     each one.
                  4. Finish Watering: Once you've watered the last (fifth) plant, you stop.
                  The loop makes sure you water each plant in turn without skipping any or watering the same one
                  twice. You just follow the same action ("Water the plant") repeatedly until all the plants have been
                  watered.
                  Different Types of Loops

                  Loop runs a statement or group of statements multiple times. There are different types of loops. Let
                  us look at them.


                       62    DigiCode AI (Ver. 2.1)-VIII
   59   60   61   62   63   64   65   66   67   68   69