Page 71 - Digicode_AI_class_7
P. 71

What is a Sequence?

            A sequence in programming is an ordered set of instructions or tasks. In case, if you follow the
            wrong order, then the task cannot be achieved, or you may get the unexpected results.
            Example of sequence is an algorithm, which is a detailed step-by-step process that needs to be
            followed in order to complete a task or to solve a problem.
            Benefits of using Sequences in coding:
               Our manner of thinking can be easily replicated in sequence programming.

               Sequence of programming reflects logical thinking.
            The above routine may vary from person to person but for a specific person this might be the routine.
            An algorithm is a process or set of rules which need to be followed to solve the given problem.
            Example: Algorithm to calculate the sum of two numbers.

             Step 1   Input first number (Num1)

             Step 2   Input second number (Num2)
             Step 3   Sum = Num1 + Num2

             Step 4   Print Sum
            In the above example, if  we swap Step 3 and Step 4, i.e. you are instructing computer to Print Sum
            first and after that add Num 1 and Num 2; the program will not give the required result or give an
            error. This is called a bug. We will discuss the bug in detail later in the chapter.

            Loop

            A loop is a construct which executes a block of code multiple times until a specific condition is met.

            Benefits:
               Reduces lines of code

               Code becomes easier to understand
            Consider an example:
            Suppose you want to print alphabets from a to d on the screen. You can do this by printing the values
            a, b, c and d by writing four lines of code like:

            Program Start                Output:
            Print a                      a
            Print b                      b
            Print c                      c

            Print d                      d
            Program End
            Doing this was easy. What if, you need to print numbers in incremental order from 1 to 1000.
            If you do this with the same method, discussed above, it will become difficult to do so. In such a
            scenario, you will use a loop to repeat the task as many times as you need.






                                                                             More on MakeCode Arcade     69
   66   67   68   69   70   71   72   73   74   75   76